Spring Beans 自动装配

末蓝、 2022-02-09 12:15 419阅读 0赞

文章目录

    • Spring Beans 自动装配
      • Spring 自动装配 `byName`
      • Spring 自动装配 `byType`
      • Spring 由构造函数自动装配

Spring Beans 自动装配

使用元素的 autowire 属性为一个 bean 定义指定自动装配模式。

Spring 自动装配 byName

这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

  • User

    public class User {

    1. private String uname;
    2. private Address address;
    3. public String getUname() {
    4. return uname;
    5. }
    6. public void setUname(String uname) {
    7. this.uname = uname;
    8. }
    9. public Address getAddress() {
    10. return address;
    11. }
    12. public void setAddress(Address address) {
    13. this.address = address;
    14. }

    }

  • Address

    public class Address {

    1. private String addr;
    2. private String tel;
    3. public String getAddr() {
    4. return addr;
    5. }
    6. public void setAddr(String addr) {
    7. this.addr = addr;
    8. }
    9. public String getTel() {
    10. return tel;
    11. }
    12. public void setTel(String tel) {
    13. this.tel = tel;
    14. }

    }

  • 配置

    <?xml version=”1.0” encoding=”UTF-8”?>













  • 测试

    @Test

    1. public void demo01(){
    2. String xmlPath = "auto.xml";
    3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    4. User user = (User) applicationContext.getBean("userId");
    5. System.out.println(user.getUname());
    6. System.out.println(user.getAddress().getAddr());
    7. System.out.println(user.getAddress().getTel());
    8. }

在这里插入图片描述

Spring 自动装配 byType

这种模式由属性类型指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。然后,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

  • 配置

    <?xml version=”1.0” encoding=”UTF-8”?>

    1. <!-- 需要包含 address 属性(即,它有一个 setAddress(...) 方法 -->
    2. <bean id="userId" class="com.cc.study.auto.User"
    3. autowire="byName">
    4. <property name="uname" value="kevin" />
    5. </bean>
    6. <!-- 这里的bean id的值必须和User的address属性相同-->
    7. <bean id="address" class="com.cc.study.auto.Address">
    8. <property name="addr" value="北京" />
    9. <property name="tel" value="110" />
    10. </bean>
  1. <bean id="userId2" class="com.cc.study.auto.User"
  2. autowire="byType">
  3. <property name="uname" value="kevin2" />
  4. </bean>
  5. </beans>
  • 测试

    @Test

    1. public void demo02(){
    2. String xmlPath = "auto.xml";
    3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    4. User user = (User) applicationContext.getBean("userId2");
    5. System.out.println(user.getUname());
    6. System.out.println(user.getAddress().getAddr());
    7. System.out.println(user.getAddress().getTel());
    8. }

在这里插入图片描述

Spring 由构造函数自动装配

这种模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常。

  • User

    public class User {

    1. private String uname;
    2. private Address address;
    3. public User(){
    4. }
    5. public User(String uname,Address address){
    6. this.uname = uname;
    7. this.address = address;
    8. }
    9. public String getUname() {
    10. return uname;
    11. }
    12. public void setUname(String uname) {
    13. this.uname = uname;
    14. }
    15. public Address getAddress() {
    16. return address;
    17. }
    18. public void setAddress(Address address) {
    19. this.address = address;
    20. }

    }

  • 配置

    <?xml version=”1.0” encoding=”UTF-8”?>

    1. <!-- 需要包含 address 属性(即,它有一个 setAddress(...) 方法 -->
    2. <bean id="userId" class="com.cc.study.auto.User"
    3. autowire="byName">
    4. <property name="uname" value="kevin" />
    5. </bean>
    6. <!-- 这里的bean id的值必须和User的address属性相同-->
    7. <bean id="address" class="com.cc.study.auto.Address">
    8. <property name="addr" value="北京" />
    9. <property name="tel" value="110" />
    10. </bean>
  1. <bean id="userId2" class="com.cc.study.auto.User"
  2. autowire="byType">
  3. <property name="uname" value="kevin2" />
  4. </bean>
  5. <bean id="userId3" class="com.cc.study.auto.User" autowire="constructor">
  6. <constructor-arg value="kevin3"/>
  7. </bean>
  8. </beans>
  • 测试

    @Test

    1. public void demo03(){
    2. String xmlPath = "auto.xml";
    3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    4. User user = (User) applicationContext.getBean("userId3");
    5. System.out.println(user.getUname());
    6. System.out.println(user.getAddress().getAddr());
    7. System.out.println(user.getAddress().getTel());
    8. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,419人围观)

还没有评论,来说两句吧...

相关阅读