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 ...
随机推荐
- oracle portlist.ini
Enterprise Manager Database Control URL - (orcl) :https://redhat4.7:1158/em [root@redhat4 install]# ...
- c# webbrowser 随机点击链接 2
找到广告代码所在的div或table ,然后用WebBrowser执行js去点这个div(或table) 那个广告是js实现的,你浏览的时候是看不到图片和连接的,请问各位大虾应该怎么实现?给点思路.. ...
- 使用Jquery promise 动态引入js文件
动态加载一个js得方式很多,如下方式: /** *一般方式加载 */ function normalLoadScript(url) { var node = document.createElemen ...
- Entity Framework查询,EF执行SQl
一.简介 EF 支持开放底层的 ADO.NET 框架,DbContext有三种常用方法 DbSet.SqlQuery //查询并返回Entities DbContext.Database.SqlQue ...
- $http POST 转字符串
- 【Java学习笔记】数组使用
package aaa; public class aaa { public static void main(String args[]) { int a[]={1,2,3,4}; for(int ...
- 【JSP】JSP动态显示时间
function showtime() { var today; var hour; var second; var minute; var year; var month; var date; va ...
- 移动设备3G网站制作的detail
说明一下,在此所说的移动设备前端开发是指针对高端智能手机(如Iphone.Android),所以需要对webkit内核的浏览器有一定的了解. 1.webkit内核中的一些私有的meta标签 <m ...
- js基础学习第一天(关于DOM和BOM)一
关于BOM和DOM BOM 下面一幅图很好的说明了BOM和DOM的关系 BOM提供了一些访问窗口对象的一些方法,我们可以用它来移动窗口位置,改变窗口大小,打开新窗口和关闭窗口,弹出对话框,进行导航以及 ...
- fastdb中的位图应用
位图内存管理: 每块内存用一个二进制位表示它的使用状态,如果该块内存被占用,则把对应位图中的对应位置1,如果空闲则置0,原理十分简单.计算机里面处理的位数最少的变量是字节(byte),所以也就是8位做 ...