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的更多相关文章

  1. 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 ...

  2. Spring Autowiring by AutoDetect

    In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...

  3. Spring Autowiring by Constructor

    In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...

  4. Spring Autowiring by Type

    In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...

  5. Spring Auto-Wiring Beans

    In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...

  6. Spring Autowiring @Qualifier example

    In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario : ...

  7. [转载]Spring Beans Auto-Wiring

    Autowiring Modes You have learnt how to declare beans using the <bean> element and inject < ...

  8. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

  9. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

随机推荐

  1. 预定义的类型“Microsoft.CSharp.RuntimeBinder.Binder”未定义或未导入

    http://www.mzwu.com/article.asp?id=3611 因为新加了Microsoft.CSharp的引用, 只需要重新生成一下项目,就可以消除这个错误提示

  2. linux下对符合条件的文件大小做汇总统计的简单命令

    (1)统计当前目录下的 *txt 文件du -c -h *txt   (2)统计当前目录下的 *txt 文件, 并求出总大小du  *txt |awk 'BEGIN{count=0;size=0;} ...

  3. TCSRM 591 div2(1000)(dp)

    挺好的dp 因为有一点限制 必须任意去除一个数 总和就会小于另一个总和 换句话来说就是去除最小的满足 那么就都满足 所以是限制最小值的背包 刚开始从小到大定住最小值来背 TLE了一组数据 后来发现如果 ...

  4. bzoj1132

    每次都选最左边的点,然后以这个点为原点 统计和这个点构成的三角形面积和 不难想到极角排序然后由叉积很容易求出 shl ; eps=1e-8; var i,j,k,m,n:longint; x,y:.. ...

  5. UITableViewCell上的按钮点击事件处理

    转自:  http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...

  6. UVA 1660 Cable TV Network 电视网络(无向图,点连通度,最大流)

    题意:给一个无向图,求其点连通度?(注意输入问题) 思路: 如果只有1个点,那么输出“1”: 如果有0条边,那么输出“0”: 其他情况:用最大流解决.下面讲如何建图: 图的连通度问题是指:在图中删去部 ...

  7. 【Mysql】初学命令行指南

    MYSQL初学者使用指南与介绍 一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbi ...

  8. 【DFS,双向】NYOJ-20-吝啬的国度

    [题目链接:NYOJ-20] 很巧妙,要好好想想 #include <iostream> #include <stdio.h> #include <vector> ...

  9. 【转】Linux Posix Timer使用

    原文网址:http://blog.csdn.net/hongszh/article/details/8608781 最强大的定时器接口来自POSIX时钟系列,其创建.初始化以及删除一个定时器的行动被分 ...

  10. 不同语言的Unix时间戳

    如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...