Spring Autowiring by Name
In Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it.
For example, if a “customer
” bean exposes an “address
” property, Spring will find the “address
” bean in current container and wire it automatically. And if no matching found, just do nothing.
You can enable this feature via autowire="byName"
like below :
<!-- customer has a property name "address" -->
<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
<bean id="address" class="com.mkyong.common.Address" >
<property name="fulladdress" value="Block A 888, CA" />
</bean>
See a full example of Spring auto wiring by name.
1. Beans
Two beans, customer
and address
.
package com.mkyong.common;
public class Customer
{
private Address address;
//...
}
package com.mkyong.common;
public class Address
{
private String fulladdress;
//...
}
2. Spring Wiring
Normally, you wire the bean explicitly, via ref
attribute like this :
<bean id="customer" class="com.mkyong.common.Customer" >
<property name="address" ref="address" />
</bean>
<bean id="address" class="com.mkyong.common.Address" >
<property name="fulladdress" value="Block A 888, CA" />
</bean>
Output
Customer [address=Address [fulladdress=Block A 888, CA]]
With autowire by name enabled, you do not need to declares the property
tag anymore. As long as the “address
” bean is same name as the property of “customer
” bean, which is “address
”, Spring will wire it automatically.
<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
<bean id="address" class="com.mkyong.common.Address" >
<property name="fulladdress" value="Block A 888, CA" />
</bean>
Output
Customer [address=Address [fulladdress=Block A 888, CA]]
See another example, this time, the wiring will failed, caused the bean “addressABC
” is not match the property name of bean “customer
”.
<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
<bean id="addressABC" class="com.mkyong.common.Address" >
<property name="fulladdress" value="Block A 888, CA" />
</bean>
Output
Customer [address=null]
Spring Autowiring by Name的更多相关文章
- Spring Auto-Wiring Beans with @Autowired annotation
In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...
- Spring Autowiring by AutoDetect
In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...
- Spring Autowiring by Constructor
In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...
- Spring Autowiring by Type
In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...
- Spring Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...
- Spring Autowiring @Qualifier example
In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario : ...
- [转载]Spring Beans Auto-Wiring
Autowiring Modes You have learnt how to declare beans using the <bean> element and inject < ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
随机推荐
- Android Dialogs(6)Dialog类使用示例:用系统theme和用自定义的theme
使用dialog时有很多 方法,其中一个就是直接 使用基类Dialog,可用来作一个没有按钮的非模态提示框,它可以直接从系统的主题构造也可从自定义的主题构造. 基本步骤: a,构造 b,调用dialo ...
- java中四种操作(dom、sax、jdom、dom4j)xml方式详解与比较
1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特 ...
- BZOJ 2154 Crash的数字表格
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){ ...
- CF 366E Dima and Magic Guitar(最远哈密顿距离)
题目链接:http://codeforces.com/problemset/problem/366/E 题意:给出一个n*m的数字矩阵A,每个矩阵元素的范围[1,K].给出一个长度为s的数字串B,B的 ...
- unity3d5.2.3中 调整视角
按住alt键不放,然后左边的手的图标会变成一个眼睛,在Scene中移动.就会发现可以调整视角了
- JSOI2008最大数(线段树)
注意到数列只增不减,而题目中又明确说道m<=200000;这样的数据规模线段树完全可以承受得了.所以我们可以事先建好一棵200000个子节点的线段树,然后求极值就好了. type node=re ...
- H.264码流结构解析
from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...
- 【流媒體】live555—VS2010 下live555编译、使用及测试
Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现了对多种音视频编 ...
- Java中实现复制文件或文件夹
拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等.但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法. [java] vi ...
- 新闻类App使用的组件
UI SlidingMenu:com.jeremyfeinstein.slidingmenu:滑动菜单 ActionBarSherlock:com.actionbarsherlock:Action B ...