Bean的自动装配(Autowired)

2023-05-07,,

Bean的自动装配(Autowired)

自动装配是Spring满足bean依赖的一种方式

Spring会在上下文中自动寻找,并自动给bean装配属性

在Spring中有三种自动装配的方式

    在xml中显示的配置

    在java中显示配置

    隐式的自动装配

Person类

 public class Person {
     private Dog dog;
     private Cat cat;
     private String name;
 ​
     public Dog getDog() {
         return dog;
    }
 ​
     public void setDog(Dog dog) {
         this.dog = dog;
    }
 ​
     public Cat getCat() {
         return cat;
    }
 ​
     public void setCat(Cat cat) {
         this.cat = cat;
    }
 ​
     public String getName() {
         return name;
    }
 ​
     public void setName(String name) {
         this.name = name;
    }
 }
 ​

    byName

 <?xml version="1.0" encoding="UTF-8"?>
 ​
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 ​
     <bean id="cat" class="Cat"/>
     <bean id="dog" class="Dog"/>
 ​
     <bean id="person" class="Person" autowire="byName">
     </bean>
 ​
 ​
 ​
 </beans>

会自动在容器上下文中差找,和自己对象set方法后面的值对应bean id

    byTyep

     <?xml version="1.0" encoding="UTF-8"?>
     ​
     <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     ​
         <bean id="cat" class="Cat"/>
         <bean id="dog222" class="Dog"/>
     ​
         <bean id="person" class="Person" autowire="byType">
         </bean>
     ​
     ​
     ​
     </beans>

    会自动在容器上下文中差找,和自己对象属性类型相同的bean,但只有保证全局只有一个时才能运行,否则会报错

        // 因为出现了两个dog type所以会报错
      <bean id="cat" class="Cat"/>
         <bean id="dog222" class="Dog"/>
         <bean id="dog2" class="Dog"/>

小结

byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set的值一致

byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

使用注解进行装配

xml注解包的导入

开启注解的支持

 <context:annotation-config/> 
 <?xml version = "1.0" encoding = "UTF-8"?>
 ​
 <beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context = "http://www.springframework.org/schema/context"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 ​
    <context:annotation-config/>// 开启注解的支持
    <!-- bean definitions go here -->
 ​
 </beans>

Person类

 import org.springframework.beans.factory.annotation.Autowired;
 ​
 public class Person {
 ​
     @Autowired
     private Dog dog;
     @Autowired
     private Cat cat;
     private String name;
 ​
     public Dog getDog() {
         return dog;
    }
 ​
 ​
     public Cat getCat() {
         return cat;
    }
 ​
 ​
     public String getName() {
         return name;
    }
 }
 ​

xml文件

 <?xml version = "1.0" encoding = "UTF-8"?>
 ​
 <beans xmlns = "http://www.springframework.org/schema/beans"
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context = "http://www.springframework.org/schema/context"
        xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 ​
     <context:annotation-config/>
 ​
     <bean id="cat" class="Cat"/>
     <bean id="dog" class="Dog"/>
     <bean id="person" class="Person"/>
 ​
 </beans>

@Autowired直接在属性上使用即可,也可以在set方式上使用

使用@Autowired我们可以不用编写Set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName

扩充

 @Nullable // 字段标记了这个注解,表示这个字段可以为null
 @Autowired(required = false)
 // 如果显式定义了Autowired的required实行为false,说明这个对象可以为null,否则不允许为空

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value="xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入

 public class Person {
 ​
     @Autowired
     @Qualifier(value="cat111")
     private Dog dog;
     @Autowired
         @Qualifier(value="dog222")
     private Cat cat;
     private String name;
  }

@Resource注解

 public class Person{
  @Resource(name = "cat2")
  private Cat cat;
 
  @Resource()
  private Dog dog
 }

小结:

@Resource和@Autowired的区别

都是用来自动装配的,都可以放在属性字段上

@Autowired通过byType的方式实现,而且必须要求这个对象存在

@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现,如果两个都找不到才报错

执行顺序不同: @Autowired通过byType的方式实现

Bean的自动装配(Autowired)的相关教程结束。

《Bean的自动装配(Autowired).doc》

下载本文的Word格式文档,以方便收藏与打印。