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 ...
随机推荐
- Xcode 项目文件介绍
一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要用来放依赖的框架 3.test文件夹是 ...
- TeeChart的X轴,使用伪装的时间
TeeChart曲线的X轴是时间,但是频率很高.没法完全显示. 例如,一秒钟有2000个点,那么点与点的间隔为0.5毫秒. 使用TChart类的GetAxisLabel事件, 函数手册上对此事件的解释 ...
- Codeforces 383A - Milking cows
原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...
- Lost connection to MySQL server at 'reading initial communication packet' 错误解决
Lost connection to MySQL server at 'reading initial communication packet' 错误解决 上次解决了这个问题,今天又碰到,突然失忆, ...
- js array 数组删除元素
/* * 方法:Array.remove(dx) * 功能:根据元素位置值删除数组元素. * 参数:元素值 * 返回:在原数组上修改数组 */ Array.prototype.baoremove = ...
- Sciter/HTMLayout内存占用评测
先从最基础的Exe文件的执行说起: Exe文件要在系统中执行,首先要将Exe文件本身加载入内存中,并且通常在内存中加载完成的Exe所占空间大小会比实际所占的磁盘空间大一些,这是由内存的特殊设定所决定的 ...
- 《C++ Primer 4th》读书笔记 第4章-数组和指针
原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3911573.html
- 一天一个Java基础——反射
1.概念 反射主要是指程序可以访问,检测和修改它本身的状态或行为的一种能力 Java中的反射是一种强大的工具,它能够创建灵活的代码,这些代码可以运行时装配,无须在组件之间进行链接 反射允许在编写与执行 ...
- 【转】《APUE》第三章笔记(4)及习题3-2
原文网址:http://www.cnblogs.com/fusae-blog/p/4256794.html APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. ...
- Java Error和Exception区别
Error和Exception都继承自Throwable: 二者不同之处: Exception: 1.可以是可被控制(checked)或者不可控制(unchecked): 2.表示一个由程序员导致的错 ...