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 ...
随机推荐
- windows和mac下分别配置虚拟主机
windows下配置 1.找到apache的配置文件,httpd.conf 2.找到 LoadModule rewrite_module modules/mod_rewrite.so 去掉前边的# 3 ...
- NuGet学习笔记
NuGet学习笔记(1)——初识NuGet及快速安装使用 NuGet学习笔记(2)——使用图形化界面打包自己的类库 NuGet学习笔记(3)——搭建属于自己的NuGet服务器
- 结构体struct和typedef后面接指针的含义
typedef struct file { ... }FileInfo, *FileP; 上述程序中定义了一个结构体,结构体的名字为file,并且给其指针 取个别名为FileP,所以后续程序中出现Fi ...
- 面试题_31_to_47_JVM 底层与GC(Garbage Collection)的面试问题
31)64 位 JVM 中,int 的长度是多数?Java 中,int 类型变量的长度是一个固定值,与平台无关,都是 32 位.意思就是说,在 32 位 和 64 位 的Java 虚拟机中,int 类 ...
- HDU 4948
题目大义: 给一张图,任意两点间有单向边,找出一种方案,使得每个新入队的点与队中的点距离<=2. 题解: 贪心,从最后入队点开始反向插入,每次找出最大入度的点入队. 只需证明最大入度点A与所有未 ...
- VB获取浏览器版本
String userAgent; userAgent = Request.UserAgent; ) { // The browser is Microsoft Internet Explorer V ...
- C#手动回收内存的简单方法
C#有自动回收内存的机制,但是有时自动回收有一定滞后,需要在变量使用后迅速回收,节约内存,这里介绍一个最简单的方法. 1.先对对象赋值 null; 2.System.GC.Collect(); 代码样 ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- Grunt + Bower—前端构建利器(转)
目前比较流行的WEB开发的趋势是前后端分离.前端采用重量级的Javascript框架,比如Angular,Ember等,后端采用restful API的Web Service服务,通过JSON格式进行 ...
- oracle等待事件以及解决方案
我们可以通过视图v$session_wait来查看系统当前的等待事件,以及与等待事件相对应的资源的相关信息,从而可确定出产生瓶颈的类型及其对象. v$session_wait的p1.p2.p3告诉我们 ...