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类
默认情况下,需要通过引用来装配bean,如下:
根据属性Property的名字装配bean,这种情况,Personr设置了autowire="byName",Spring会自动寻找与属性名字“student”相同的bean,找到后,通过调用setStudent(Student
student)将其注入属性。
Property name找不到对应的bean配置,如下
根据属性Property的数据类型自动装配,这种情况,Person设置了autowire="byType",Spring会总动寻找与属性类型相同的bean,找到后,通过调用setStudent(Student
student)将其注入。
如果配置文件中有两个类型相同的bean会怎样呢?如下:
一旦配置如上,有两种相同数据类型的bean被配置,将抛出UnsatisfiedDependencyException异常:
所以,一旦选择了’byType’类型的自动装配,请确认你的配置文件中每个数据类型定义一个唯一的bean。
这种情况下,Spring会寻找与参数数据类型相同的bean,通过构造函数public Person(Student
student)将其注入
这种情况下,Spring会先寻找Person中是否有默认的构造函数,如果有相当于上边的’constructor’这种情况,用构造函数注入,否则,用’byType’这种方式注入;
注意: 自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,
容易引发一些潜在的错误,在实际项目中要谨慎使用。
由于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(Studentstudent)将其注入属性。
<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(Studentstudent)将其注入。
<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(Studentstudent)将其注入
<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>
注意: 自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,
容易引发一些潜在的错误,在实际项目中要谨慎使用。
相关文章推荐
- Java的MyBatis框架中实现多表连接查询和查询结果分页
- JAVA多线程实现的三种方式
- Java模拟登录正方教务抓取成绩、课表、空教室
- Spring
- java 判断一个数是否为“开心数”
- Sublime Text3 设置Java开发环境,Javatar
- JAVA通信的三种I/O模型
- JAVA WEB 总结 Servlet篇--源自技术
- SpringMVC的静态资源无法请求到的解决办法
- Spring + Quartz CronExpression静态及动态定时任务
- SSH多个数据源动态数据切换
- Java中的内部类
- Java 环境搭建
- java.util.concurrent.Phaser理解
- java实现简单注册选择所在城市
- java中的多线程包--- Timer的缺陷 用ScheduledExecutorService替代
- 通过终端命令行编译java文件
- JAVA AOP--源自技术
- java 里面for (String str : s){}怎么理解
- Java中ArrayList和LinkedList区别