Bean的自动装配
自动装配说明
自动装配是使用spring满足bean依赖的一种方法
spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:
1.在xml中显式配置;
2.在java中显式配置;
3.隐式的bean发现机制和自动装配。
这里我们主要讲第三种:自动化的装配bean。
Spring的自动装配需要从两个角度来实现,或者说是两个操作:
1.组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
2.自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。
推荐不使用自动装配xml配置 , 而使用注解 .
————————————————————————————————————————————————————
测试环境搭建
1、新建一个项目
2、新建两个实体类,Cat Dog 都有一个叫的方法
package com.lyh.pojo;
public class Car {
public void shout(){
System.out.println("喵喵");
}
}
package com.lyh.pojo;
public class Dog {
public void shout(){
System.out.println("汪汪");
}
}
3、新建一个用户类 User
package com.lyh.pojo;
public class People {
private Car car;
private Dog dog;
private String name;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
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{" +
"car=" + car +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
4、编写Spring配置文件
<?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="car" class="com.lyh.pojo.Car"/>
<bean id="dog" class="com.lyh.pojo.Dog"/>
<bean id="people" class="com.lyh.pojo.People">
<property name="name" value="梁元浩"></property>
<property name="car" ref="cat"></property>
<property name="dog" ref="dog"></property>
</bean>
</beans
5、测试
import com.lyh.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCar().shout();
}
}
结果正常输出,环境OK
————————————————————————————————————————————————————
byName
autowire byName (按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。
采用自动装配将避免这些错误,并且使配置简单化。
测试:
1、修改bean配置,增加一个属性 autowire=“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.xsd">
<bean id="car" class="com.lyh.pojo.Car"/>
<bean id="dog" class="com.lyh.pojo.Dog"/>
<bean id="people" class="com.lyh.pojo.People" autowire="byName">
<property name="name" value="梁元浩"></property>
</bean>
</beans>
2、再次测试,结果依旧成功输出!
3、我们将 cat 的bean id修改为 catXXX
4、再次测试, 执行时报空指针java.lang.NullPointerException。因为按byName规则找不对应set方法,真正的setCat就没执行,对象就没有初始化,所以调用时就会报空指针错误。
小结:
1.当一个bean节点带有 autowire byName的属性时。
2.将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
3.去spring容器中寻找是否有此字符串名称id的对象。
如果有,就取出注入;如果没有,就报空指针异常。
——————————————————————————————————————————————————————————————
byType
1、修改bean配置,增加一个属性 autowire=“byType”
<?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="car" class="com.lyh.pojo.Car"/>
<bean id="dog" class="com.lyh.pojo.Dog"/>
<bean id="people" class="com.lyh.pojo.People" autowire="byType">
<property name="name" value="梁元浩"></property>
</bean>
</beans>
使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
NoUniqueBeanDefinitionException
测试:
1、将user的bean配置修改一下 : autowire=“byType”
2、测试,正常输出
3、在注册一个cat 的bean对象!
<bean id="dog" class="com.lyh.pojo.Dog"/>
<bean id="cat" class="com.lyh.pojo.Car"/>
<bean id="cat2" class="com.lyh.pojo.Cat"/>
<bean id="user" class="com.lyh.pojo.People" autowire="byType">
<property name="name" value="梁元浩"/>
</bean>
4、测试,报错:NoUniqueBeanDefinitionException
5、删掉cat2,将cat的bean名称改掉!测试!因为是按类型装配,所以并不会报异常,也不影响最后的结果。甚至将id属性去掉,也不影响结果。
这就是按照类型自动装配!