Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean
1. Auto-Wiring ‘no’
2. Auto-Wiring ‘byName’
3. Auto-Wiring ‘byType
4. Auto-Wiring ‘constructor’
5. Auto-Wiring ‘autodetect’
Spring Auto-Wiring Beans——Spring自动装配Bean
所谓自动装配,就是将一个Bean注入到其他Bean的Property中,类似于以下:
<bean id="customer" class="com.lei.common.Customer" autowire="byName" />
Spring支持5种自动装配模式,如下:
no ——默认情况下,不自动装配,通过“ref”attribute手动设定。
buName ——根据Property的Name自动装配,如果一个bean的name,和另一个bean中的Property的name相同,则自动装配这个bean到Property中。
byType ——根据Property的数据类型(Type)自动装配,如果一个bean的数据类型,兼容另一个bean中Property的数据类型,则自动装配。
constructor ——根据构造函数参数的数据类型,进行byType模式的自动装配。
autodetect ——如果发现默认的构造函数,用constructor模式,否则,用byType模式。
下例中演示自动装配
Customer.java如下:
package com.lei.common; public class Customer
{
private Person person; public Customer(Person person) {
this.person = person;
} public void setPerson(Person person) {
this.person = person;
}
//...
}
Person.java如下:
package com.lei.common; public class Person
{
//...
}
1. Auto-Wiring ‘no’
默认情况下,需要通过'ref’来装配bean,如下:
<bean id="customer" class="com.lei.common.Customer">
<property name="person" ref="person" />
</bean>
<bean id="person" class="com.lei.common.Person" />
2. Auto-Wiring ‘byName’
根据属性Property的名字装配bean,这种情况,Customer设置了autowire="byName",Spring会自动寻找与属性名字“person”相同的bean,找到后,通过调用setPerson(Person person)将其注入属性。
<bean id="customer" class="com.lei.common.Customer" autowire="byName" /> <bean id="person" class="com.lei.common.Person" />
如果根据 Property name找不到对应的bean配置,如下
<bean id="customer" class="com.lei.common.Customer" autowire="byName" /> <bean id="person_another" class="com.lei.common.Person" />
Customer中Property名字是person,但是配置文件中找不到person,只有person_another,这时就会装配失败,运行后,Customer中person=null。
3. Auto-Wiring ‘byType
根据属性Property的数据类型自动装配,这种情况,Customer设置了autowire="byType",Spring会总动寻找与属性类型相同的bean,找到后,通过调用setPerson(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="byType" /> <bean id="person" class="com.lei.common.Person" />
如果配置文件中有两个类型相同的bean会怎样呢?如下:
<bean id="customer" class="com.lei.common.Customer" autowire="byType" /> <bean id="person" class="com.lei.common.Person" /> <bean id="person_another" class="com.lei.common.Person" />
一旦配置如上,有两种相同数据类型的bean被配置,将抛出UnsatisfiedDependencyException异常,见以下:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
所以,一旦选择了’byType’类型的自动装配,请确认你的配置文件中每个数据类型定义一个唯一的bean。
4. Auto-Wiring ‘constructor’
这种情况下,Spring会寻找与参数数据类型相同的bean,通过构造函数public Customer(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="constructor" /> <bean id="person" class="com.lei.common.Person" />
5. Auto-Wiring ‘autodetect’
这种情况下,Spring会先寻找Customer中是否有默认的构造函数,如果有相当于上边的’constructor’这种情况,用构造函数注入,否则,用’byType’这种方式注入,所以,此例中通过调用public Customer(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="autodetect" /> <bean id="person" class="com.lei.common.Person" />
注意:
项目中autowire结合dependency-check一起使用是一种很好的方法,这样能够确保属性总是可以成功注入。
<bean id="customer" class="com.lei.common.Customer" autowire="autodetect" dependency-check="objects" /> <bean id="person" class="com.lei.common.Person" />
最后,我认为,自动装配虽然让开发变得更快速,但是同时却要花更大的力气维护,因为它增加了配置文件的复杂性,你甚至不知道哪一个bean会被自动注入到另一个bean中。我更愿意写配置文件来手工装配。
Spring3系列8- Spring 自动装配 Bean的更多相关文章
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring自动装配Bean的五种方式
在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...
- Spring自动装配bean
Spring推荐面向接口编程,这样可以很好的解耦具体的实现类. CompactDisc.class 文件: public interface CompactDisc { void play(); } ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean
除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...
- spring 自动装配 bean 有哪些方式?
Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...
- Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
- Spring的自动装配Bean
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...
随机推荐
- iOS URL 编码
一.iOS 中的NSURL编码 iOS 中,NSURL 的基本样式是 scheme://username:password@host:port/path?query#fragment RFC 1738 ...
- 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数 其各位数字立方和等于该数本身。 例如:153是一个 "水仙花数 " 因为153=1*1*1+5*5*5+3*3*3
for (int i = 100; i <= 999; i++) { int geWei, shiWei, baiWei; baiWei = i / 100; shiWei = (i - bai ...
- Dynamic CRM 2013学习笔记(二十三)CRM JS智能提示(CRM 相关的方法、属性以及页面字段),及发布前调试
我们知道在CRM的js文件里引用XrmPageTemplate.js后,就可以实现智能提示,但每个js文件都引用太麻烦了,其实可以利用vs的功能让每个js文件自动实现智能提示CRM的js: 另外,我们 ...
- Javascript中new Date的坑
在一段判断是否过期的js代码中是这么写的: if (new Date() < new Date(2014, 9, 25)) { //... } 后来发现过了9月25日竟然不过期,console. ...
- 在Visio2010中修改默认字体的大小
由于我常需要在Visio2010中画流程图和UML图,但是Visio2010中的字体默认8px,这对眼睛是个挑战.摸索了好久终于找到在visio2010中修改字体大小的方式. 1.点一下红色箭头所指的 ...
- jquery bind、delegate、live、on的区别及联系
jquery bind.delegate.live.on的区别及联系 概述 jquery提供了好几个API都可以实现事件绑定, 如 delegate, live , bind 等, 可是有没有疑惑这几 ...
- [JS7] 显示从0到99的100个数字
<html> <head> <title>JS Unleashed</title> </head> <body> <SCR ...
- Linux:常用shell快捷键
按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂定当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,恢复到前台为命令fg Ctrl+a 将光标移至输入行头,相 ...
- mysql悲观锁总结和实践
使用场景举例:以MySQL InnoDB为例商品t_goods表中有一个字段status,status为1代表商品未被下单,status为2代表商品已经被下单,那么我们对某个商品下单时必须确保该商品s ...
- atitit.查看预编译sql问号 本质and原理and查看原生sql语句
atitit.查看预编译sql问号 本质and原理and查看原生sql语句 1. 预编译原理. 1 2. preparedStatement 有三大优点: 1 3. How to look gene ...