Bean的自动装配
- 自动装配式Spring满足bean依赖一种方式
- Spring会在上下文中自动寻找,并自动给Bean装配属性
自动装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配
在xml中显示的配置
编写ca、dog、porple类 在porple中有
private Cat cat;
private Dog dog;
属性
xml自动装配(autowire):
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="cn.etguoke.pojo.Cat"/>
<bean id="dog" class="cn.etguoke.pojo.Dog"/>
<bean id="people" class="cn.etguoke.pojo.People" autowire="byName"/>
</beans>
autowire属性
- byName:和自己对象set方法后面的值对应 需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致
- byType 和自己对象属性相同的bean 需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
使用注解自动装配配置
jdk1.5支持注解,Spring2.5就支持注解了
- 导入约束 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>
使用
- 在xml中注册bean
<?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/>
<bean id="cat" class="cn.etguoke.pojo.Cat"/>
<bean id="dog" class="cn.etguoke.pojo.Dog"/>
<bean id="dog1" class="cn.etguoke.pojo.Dog"/>
<bean id="people" class="cn.etguoke.pojo.People"/>
</beans>
- 在类中添加注解
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
public class People {
// required这个属性的值可以为null
@Autowired(required = false)
private Cat cat;
@Autowired
@Qualifier(value = "dog1")
private Dog dog;
private String name;
public String getName() {
return name;
}
public void setName(@Nullable String name) {
this.name = 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;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
'}';
}
}
- Autowired:直接在属性或set方法上使用
- Nullable:可以为空值
- Autowired(required = false):可以为空值
- Qualifier(value = “dog1”):当环境比较复杂时可以使用 @Qualifier(value = “xxx”)去配和Autowired使用,指定一个唯一的bean对象
使用java的注解也可以实现自动装配
- Resource :直接在属性或set方法上使用
与Autowired的区别:
- 都时用来自动装配的,都可以放在属性字段上
- Autowired 默认通过byType的方式实现 xml可以 “byName||byType”
- Resource:默认通过byName的方式实现,如果找不到名字则通过byType