自动装配说明
- 自动装配是使用spring满足bean依赖的一种方法
- spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:
-
在xml中显式配置;
-
在java中显式配置;
-
隐式的bean发现机制和自动装配。【重要】
1. 测试
- 搭建环境
public class Cat {
public void shout(){
System.out.println("喵~");
}
}
public class Dog {
public void shout(){
System.out.println("汪~");
}
}
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
<bean id="cat" class="com.qk.pojo.Cat"></bean>
<bean id="dog" class="com.qk.pojo.Dog"></bean>
<bean id="people" class="com.qk.pojo.People">
<property name="name" value="qk"></property>
<property name="cat" ref="cat"></property>
<property name="dog" ref="dog"></property>
</bean>
2. ByName自动装配
- 会自动在容器上下文中查找和自己对象set方法后面的值对应的bean id
<bean id="cat" class="com.qk.pojo.Cat"></bean>
<bean id="dog" class="com.qk.pojo.Dog"></bean>
<bean id="people" class="com.qk.pojo.People" autowire="byName">
<property name="name" value="qk"></property>
</bean>
小结:
- 当一个bean节点带有 autowire byName的属性时。将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。去spring容器中寻找是否有此字符串名称id的对象。 如果有,就取出注入;如果没有,就报空指针异常。
- 必须保证bean id 的一致性,且区分大小写,如果写错,就不能自动装配。
3. ByType自动装配
- 会自动在容器上下文中查找和自己对象属性相同的bean
<bean id="people" class="com.qk.pojo.People" autowire="byType">
<property name="name" value="qk"></property>
</bean>
小结:
- 使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
- 哪怕省略bean 的id也可以成功注入
总结:
- Byname的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
- Bytype的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
4. 使用注解实现自动装配
使用注解须知:
- 导入约束
xmlns:context="http://www.springframework.org/schema/context"
和
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
- 配置注解的支持
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
- 一般用在实体类属性上
- set方法上也可以成功注入
- set方法都可以不要,也可以成功注入,只留get方法和toString
科普:
- @Nullable 字段标记了这个注解,说明这个字段可以为null
- @Autowired(required = false) required = false说明这个对象可以为null,否则不允许为空。(bean没有定义也不报错)
- @Autowired默认按照类型(ByType)注入,不支持id匹配。
- 如果想要按照名称(ByName),需要结合@Qualifier
- 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired来完成的时候,可以使用@Qualifier(value=“xxx”)去配合@Autowired使用,指定唯一的bean对象注入。
@Resource
-
@Resource如有指定的name属性,先按该属性进行byName方式查找装配;
-
其次再进行默认的byName方式进行装配;
-
如果以上都不成功,则按byType的方式自动装配。
-
都不成功,则报异常。
- 没有name的指定的情况下,先根据ByName装配,再根据ByType装配
小结:
-
@Resource和@Autowired的区别
-
@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
-
@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
-
@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配
-
它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。
-