(一) 为何学习Spring的自动装配?
由于set注入和构造函数注入时,在配置文件中写的比较多。所以框架为了提高开发效率,提供自动装配功能,简化配置。Spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean>标签的autowire属性
(二)Spring中有五种自动装配类型:no,byName,byType,constructor,default
no ——默认情况下,不自动装配,通过“ref”attribute手动设定。
buName ——根据Property的Name自动装配,如果一个bean的name,和另一个bean中的Property的name相同,则自动装配这个bean到Property中。
byType ——根据Property的数据类型(Type)自动装配,如果一个bean的数据类型,兼容另一个bean中Property的数据类型,则自动装配。
constructor ——根据构造函数参数的数据类型,进行byType模式的自动装配。
default ——如果发现默认的构造函数,用constructor模式,否则,用byType模式。
(三)实现自动装配
Person类
public class Person {
private Student student;
public Person() {
super();
}
public Person(Student student) {
this.student = student;
}
private String pname; // String类型
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
Student类
public class Student{
public void say(){
System.out.println("say spring autowire!");
}
}
1. Auto-Wiring ‘no’
默认情况下,需要通过引用来装配bean,如下:
<bean id="person" class="com.hlx.autowire.Person">
<property name="student" ref="student" />
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>
2. Auto-Wiring ‘byName’
根据属性Property的名字装配bean,这种情况,Personr设置了autowire="byName",Spring会自动寻找与属性名字“student”相同的bean,找到后,通过调用setStudent(Student student)将其注入属性。
<bean id="person" class="com.hlx.autowire.Person" autowire="byName">
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>
如果根据 Property name找不到对应的bean配置,如下
<bean id="person" class="com.hlx.autowire.Person" autowire="byName">
<property name="pname" value="sa" />
</bean>
<bean id="student1" class="com.hlx.autowire.Student"></bean>
Person中Property名字是student,但是配置文件中找不到student,只有student1这时就会装配失败,运行后,Person中student=null。
3. Auto-Wiring ‘byType
根据属性Property的数据类型自动装配,这种情况,Person设置了 autowire="byType" ,Spring会总动寻找与属性类型相同的bean,找到后,通过调用 setStudent(Student student) 将其注入。<pre name="code" class="html"><bean id="person" class="com.hlx.autowire.Person" autowire="byType">
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>
如果配置文件中有两个类型相同的bean会怎样呢?如下:
<bean id="person" class="com.hlx.autowire.Person" autowire="byType">
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>
<bean id="student1" class="com.hlx.autowire.Student"></bean>
一旦配置如上,有两种相同数据类型的bean被配置,将抛出UnsatisfiedDependencyException异常:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
所以,一旦选择了’byType’类型的自动装配,请确认你的配置文件中每个数据类型定义一个唯一的bean。
4. Auto-Wiring ‘constructor’
<bean id="person" class="com.hlx.autowire.Person" autowire="constructor">
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>
5. Auto-Wiring ‘default’
这种情况下,Spring会先寻找Person中是否有默认的构造函数,如果有相当于上边的’constructor’这种情况,用构造函数注入,否则,用’byType’这种方式注入;
<bean id="person" class="com.hlx.autowire.Person" autowire="default">
<property name="pname" value="sa" />
</bean>
<bean id="student" class="com.hlx.autowire.Student"></bean>