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中。我更愿意写配置文件来手工装配。

最新文章

  1. Datazen配置
  2. 如何提升我的HTML&amp;CSS技术,编写有结构的代码
  3. python2.7.6 , setuptools pip install, 报错:UnicodeDecodeError:&#39;ascii&#39; codec can&#39;t decode byte
  4. ubuntu自带的gedit编辑器添加Markdown预览插件
  5. 数组添加:如何往数组的&quot;null&quot;位置插入数据呢?
  6. MySQL中别名的使用
  7. 关于SSH整合中对于Hibernate的Session关闭的问题
  8. BZOJ 2243: [SDOI2011]染色 (树链剖分+线段树合并)
  9. 1040: [ZJOI2008]骑士 - BZOJ
  10. 数据结构与算法JavaScript 读书笔记
  11. JavaScript字符串转换成数字的三种方法
  12. 搭建Hadoop的环境
  13. 一个可以自动生成css样式的插件happycss
  14. Vue(五) 购物车案例
  15. effective java——12考虑实现coparable接口
  16. weblogic系列漏洞整理 -- 1. weblogic安装
  17. CSS 列表实例
  18. 【Sqlserver】SqlServer中EXEC 与 SP_EXECUTESQL的 区别
  19. redis水平扩展实践,完全配置,无需代码改动
  20. springboot项目中报错:listener does not currently know of SID given in connect descriptor

热门文章

  1. 将Nagios监控信息存入Mysql
  2. ectouch第四讲 之缓存文件的生成
  3. LeetCode----204. Count Primes(Java)
  4. jqeury之轮播图
  5. CRB and String
  6. 《python核心编程》读书笔记--第16章 网络编程
  7. JAVA基础知识之网络编程——-TCP/IP协议,socket通信,服务器客户端通信demo
  8. java 导入自定义类
  9. laravel框架总结(二) -- blade模板引擎
  10. mongodb(4查询)