In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>.

	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />

In Spring, 5 Auto-wiring modes are supported.

  • no – Default, no auto wiring, set it manually via “ref” attribute
  • byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.
  • byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.
  • constructor – byType mode in constructor argument.
  • autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.

Examples

A Customer and Person object for auto wiring demonstration.

package com.mkyong.common;

public class Customer
{
private Person person; public Customer(Person person) {
this.person = person;
} public void setPerson(Person person) {
this.person = person;
}
//...
}
package com.mkyong.common;

public class Person
{
//...
}

1. Auto-Wiring ‘no’

This is the default mode, you need to wire your bean via ‘ref’ attribute.

	<bean id="customer" class="com.mkyong.common.Customer">
<property name="person" ref="person" />
</bean> <bean id="person" class="com.mkyong.common.Person" />

2. Auto-Wiring ‘byName

Auto-wire a bean by property name. In this case, since the name of “person” bean is same with the name of the “customer” bean’s property (“person”), so, Spring will auto wired it via setter method – “setPerson(Person person)“.

	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />

	<bean id="person" class="com.mkyong.common.Person" />

3. Auto-Wiring ‘byType

Auto-wire a bean by property data type. In this case, since the data type of “person” bean is same as the data type of the “customer” bean’s property (Person object), so, Spring will auto wired it via setter method – “setPerson(Person person)“.

	<bean id="customer" class="com.mkyong.common.Customer" autowire="byType" />

	<bean id="person" class="com.mkyong.common.Person" />

4. Auto-Wiring ‘constructor

Auto-wire a bean by property data type in constructor argument. In this case, since the data type of “person” bean is same as the constructor argument data type in “customer” bean’s property (Person object), so, Spring auto wired it via constructor method – “public Customer(Person person)“.

	<bean id="customer" class="com.mkyong.common.Customer" autowire="constructor" />

	<bean id="person" class="com.mkyong.common.Person" />

5. Auto-Wiring ‘autodetect’

If a default constructor is found, uses “constructor”; Otherwise, uses “byType”. In this case, since there is a default constructor in “Customer” class, so, Spring auto wired it via constructor method – “public Customer(Person person)“.

	<bean id="customer" class="com.mkyong.common.Customer" autowire="autodetect" />

	<bean id="person" class="com.mkyong.common.Person" />

Note

. It’s always good to combine both ‘auto-wire’ and ‘dependency-check’ together, to make sure the property is always auto-wire successfully.

	<bean id="customer" class="com.mkyong.common.Customer"
autowire="autodetect" dependency-check="objects /> <bean id="person" class="com.mkyong.common.Person" />

Conclusion

In my view, Spring ‘auto-wiring’ make development faster with great costs – it added complexity for the entire bean configuration file, and you don’t even know which bean will auto wired in which bean.

In practice, i rather wire it manually, it is always clean and work perfectly, or better uses @Autowired annotation, which is more flexible and recommended.

Spring Auto-Wiring Beans的更多相关文章

  1. Spring Auto scanning components

    Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...

  2. Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件

    一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Components Manually——手动配置componen ...

  3. Spring框架之beans源码完全解析

    导读:Spring可以说是Java企业开发里最重要的技术.而Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Oriented Programmin ...

  4. Spring Auto proxy creator example

    In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...

  5. spring入门:beans.xml不提示、别名、创建对象的三种方式

    spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...

  6. spring框架中beans.xml文件报错XmlBeanDefinitionStoreException

    第一次构建spring,实现简单的注入方式,就发生了beans.xml文件报错,报错信息如下图 org.springframework.beans.factory.xml.XmlBeanDefinit ...

  7. Spring框架配置beans.xml扩展

    Spring学习笔记(二) 续Spring 学习笔记(一)之后,对Spring框架XML的操作进行整理 1 什么是IOC(DI) IOC = inversion of control   控制反转 D ...

  8. Spring框架配置beans.xml

    Spring学习笔记(一) 一.Spring 框架 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 ...

  9. Spring 配置文件XML -- <beans>中属性概述

    beans : xml文件的根节点. xmlns : XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上 ...

  10. Spring学习(三)-----Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

随机推荐

  1. tranform-scale 缩小元素,移上去文字抖动

    元素缩小后,鼠标移上去之后文字会出现抖动, -webkit-transform:scale(0.5); 修复代码如下: *{ -webkit-backface-visibility: hidden; ...

  2. th固定 td滚动的表格实现

    为什么这样? 体验好 原理 通过两个表格,使其th td 对应,产生一种错觉. 代码 1.html <div class="content"> <div clas ...

  3. HDU 1233 还是畅通工程(最小生成树,prim)

    题意:中文题目 思路:prim实现,因为有n*(n-1)/2条边,已经是饱和的边了,prim比较合适. (1)将点1置为浏览过,点1可以到达其他每个点,所以用low[i]数组记录下目前到达i点的最小长 ...

  4. vim - 查找替换

    :%s/\<key_word_replaced\>/word_you_want_to_say/g

  5. VS2010下编译安装DarwinStreamingServer5.5.5

    源码下载链接:http://dss.macosforge.org/源码版本: 5.5.5版本电脑环境:visual studio2010,window 7 x64系统.用VS2010打开WinNTSu ...

  6. 【转】终于解决了Apache乱码问题

    之前开放了一个空间,给网友提供电台节目音频下载.由于多年节目的文件数量甚多,且分类没有特定格式,图省事,没有制作网页提供分类下载,而是直接利用Apache的目录浏览功能,简单直观. 不过,所在的美国服 ...

  7. 值班问题:insert语句插入了两条数据?

    上周值班,碰到这样的一个客户问题,表结构简化如下: CREATE TABLE `aa` (`c1` int(10) unsigned NOT NULL AUTO_INCREMENT,`c2` int( ...

  8. 【c++内存分布系列】单独一个类

    首先要明确类型本身是没有具体地址的,它是为了给编译器生成相应对象提供依据.只有编译器生成的对象才有明确的地址. 一.空类 形如下面的类A,类里没有任何成员变量,类的sizeof值为1. #includ ...

  9. 判断DataSet是否有数据

    if (data1.Tables[0].Rows.Count>0)        {            MessageInfoText.Text = data1.Tables[0].Rows ...

  10. POJ 2481 Cows

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16546   Accepted: 5531 Description ...