Spring Auto-Wiring Beans
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的更多相关文章
- Spring Auto scanning components
Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...
- Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件
一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Components Manually——手动配置componen ...
- Spring框架之beans源码完全解析
导读:Spring可以说是Java企业开发里最重要的技术.而Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Oriented Programmin ...
- Spring Auto proxy creator example
In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...
- spring入门:beans.xml不提示、别名、创建对象的三种方式
spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...
- spring框架中beans.xml文件报错XmlBeanDefinitionStoreException
第一次构建spring,实现简单的注入方式,就发生了beans.xml文件报错,报错信息如下图 org.springframework.beans.factory.xml.XmlBeanDefinit ...
- Spring框架配置beans.xml扩展
Spring学习笔记(二) 续Spring 学习笔记(一)之后,对Spring框架XML的操作进行整理 1 什么是IOC(DI) IOC = inversion of control 控制反转 D ...
- Spring框架配置beans.xml
Spring学习笔记(一) 一.Spring 框架 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 ...
- Spring 配置文件XML -- <beans>中属性概述
beans : xml文件的根节点. xmlns : XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上 ...
- Spring学习(三)-----Spring自动装配Beans
在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...
随机推荐
- View的个得区域函数getHitRect,getDrawingRect,getLocalVisibleRect,getGlobalVisibleRect(*)
注意: OnCreate()函数中 调用下面函数,结果全为0,要等UI控件都加载完了才能得到绘制时的值. getHitRect 以父控件的左上为原点,计算当前view在父控件的区域,不管父控件在屏幕的 ...
- 【转载】React入门-Todolist制作学习
我直接看的这个React TodoList的例子(非常好!): http://www.reqianduan.com/2297.html 文中示例的代码访问路径:http://127.0.0.1:708 ...
- 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories
The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk ...
- linux文件和目录基本操作
比较特殊的目录: . 代表此层目录 .. 代表上一层目录 - 代表前一个工作目录 -代表当前用户身份所在的主文件夹 -account 代表account用户所在主文件夹 1.目录相关操作 cd切换 ...
- mssql修改链接数为默认值
EXEC sys.sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDE GO EXEC sys.sp_confi ...
- jquery dialog-优雅的弹出框
前面一章已经对datepicker的使用,做了简单的说明.这一章主要对dialog如何使用做个说明. jquery ui-dialog在web开发中运用还是比较多的.最常见的例子就是登 ...
- BZOJ 2179 FFT快速傅里叶
fft. #include<set> #include<map> #include<ctime> #include<queue> #include< ...
- vs2013编译boost库
打开vs2013>>visual studio tools>>VS2013 x64 本机工具命令提示 cd D:\lib\boost_1_55_0\boost_1_55_0 b ...
- cocos2d-x中的Jni使用(C++与Andriod方法互调)
作者:何卫 转载请注明,原文链接:http://www.cnblogs.com/hewei2012/p/3376616.html 前提条件: 1.操作的游戏工程和cocos2d_x游戏引擎是一个目录的 ...
- 她让我懂得了怎样学习Flash
原文:http://www.asv5.cn/blog/article.asp?id=169 最近忙着寻找两样丢失了很久的东西,都是她帮我找回来的,第一样叫做自信,第二样叫做梦想.也正因为有了她,我才从 ...