七、Bean的自动装配
- 自动装配是Spring满足Bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给Bean装配属性
在Spring中有三种装配的方式
- 在xml中显示的装配
- 在java中显示的配置
- 隐式的自动装配Bean
7.1、环境搭建
public class People {
private String name;
private Cat cat;
private Dog dog;
public String getName() {
return name;
}
public void setName(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{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
public class Cat {
public void shout() {
System.out.println("miaomiao~~~~");
}
}
public class Dog {
public void shout() {
System.out.println("wangwang~~~~");
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.zhang.pojo.Cat"/>
<bean id="dog" class="com.zhang.pojo.Dog"/>
<bean id="people" class="com.zhang.pojo.People">
<property name="name" value="张三"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
测试类
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();//wangwang~~~~
people.getCat().shout();//miaomiao~~~~
}
7.2、autowire自动装配
byName
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="people" class="com.zhang.pojo.People" autowire="byName">
<property name="name" value="张三"/>
</bean>
必须保证寻找的名字和set后的名字一致,否则报错 也就是bean中如果id和寻找的不一样就报错
byType
<!--
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.zhang.pojo.People" autowire="byType">
<property name="name" value="张三"/>
</bean>
必须保证寻找的类型全局唯一
总结:
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
- byType的时候,需要保证所有class唯一,并且这个bean需要和自动注入的属性的类型一致
7.3、使用注解实现自动装配
基于注释的配置的引入提出了一个问题,即这种方法是否比XML"更好"。简短的回答是"视情况而定"。长的回答是,每种方法都有其优缺点,通常由开发人员决定哪种策略更适合他们。由于它们的定义方式,注释在声明中提供了大量上下文,从而导致更简洁的配置。但是,XML 擅长在不接触其源代码或重新编译组件的情况下连接组件。一些开发人员喜欢将线路靠近源,而另一些开发人员则认为带注释的类不再是 POJOs,此外,配置变得分散且难以控制。
要使用注解须知:
-
导入约束 context约束
-
配置注解的支持
<?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
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
@Autowired(required = false)
如果设置了required的属性为false,说明这个对象可以为null,否则不允许为null
如果Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value = “xxx” )配合@Autowired的使用,指定一个唯一的Bean对象的注入
@Autowired
@Qualifier(value = "cat1" )
private Cat cat;
@Resource
@Resource(name = "cat1")
private Cat cat;
小结:
@Autowired和@Resource的区别:
- 都是用用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要要求这个对象存在,空指针
- @Resource默认通过byName的方式实现,如果找不到名字,则通过byType的方式实现,如果两个都找不到的情况下,就报错!
- 执行顺序不同: