7、bean的自动装配
- 自动装配是Spring满足bean依赖一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配bean【重要】
7.1、测试
环境搭建:一个人有两个宠物
7.2、ByName自动装配
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!
-->
<bean id="people" class="com.maple.pojo.People" autowire="byName">
<property name="name" value="maple"/>
</bean>
7.3、ByType自动装配
<bean class="com.maple.pojo.Cat"/>
<bean class="com.maple.pojo.Dog"/>
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!
-->
<bean id="people" class="com.maple.pojo.People" autowire="byType">
<property name="name" value="maple"/>
</bean>
小结:
- ByName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性set方法一致!
- ByType的时候,需要保证所有bean的Class唯一,并且这个bean需要和自动注入的属性的类型一致!
7.4、使用注解实现自动装配
jdk1.5之后支持注解,spring2.5后就支持了
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.
要使用注解须知:
- 导入约束 context约束
- 配置注解的支持 :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方法是使用!
使用@Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(spring)容器中存在,并且符合byname!
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
测试代码:
public class People {
//如果显示的标识了required = false说明了字段可以为null
@Autowired(required = false)
private Cat cat;
@Autowired
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 + '\'' +
'}';
}
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成时,我们可以使用@Qualifier(value = “xxx”)去配合@Autowired使用,指定一个唯一的bean注入!
public class People {
//如果显示的标识了required = false说明了字段可以为null
@Autowired(required = false)
private Cat cat;
@Autowired
@Qualifier(value = "dog")
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 + '\'' +
'}';
}
}
@Resource注解
public class People {
@Resource
private Cat cat;
@Resource
private Dog dog;
private String name;
}
小结:
@Autowired和@Resource的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过ByType方式实现
- @Resource默认通过byName方式实现,如果找不到名字,则通过ByType实现!如果二个都找不到就报错【常用】
- 执行顺序不同:@Autowired通过ByType方式实现。@Resource默认通过byName方式实现。