您的位置:首页 > 编程语言 > Java开发

Spring框架自动装配 Bean(三)

2016-04-06 15:44 399 查看
(一) 为何学习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’

这种情况下,Spring会寻找与参数数据类型相同的bean,通过构造函数public Person(Student
student)将其注入

<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>



注意: 自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,

容易引发一些潜在的错误,在实际项目中要谨慎使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: