In Spring, “Autowiring by Constructor” is actually autowiring by Type in constructor argument. It means, if data type of a bean is same as the data type of other bean constructor argument, auto wire it.

See a full example of Spring auto wiring by constructor.

1. Beans

Two beans, developer and language.

package com.mkyong.common;

public class Developer {
private Language language; //autowire by constructor
public Developer(Language language) {
this.language = language;
} //... }
package com.mkyong.common;

public class Language {
private String name;
//...
}

2. Spring Wiring

Normally, you wire the bean via constructor like this :

	<bean id="developer" class="com.mkyong.common.Developer">
<constructor-arg>
<ref bean="language" />
</constructor-arg>
</bean> <bean id="language" class="com.mkyong.common.Language" >
<property name="name" value="Java" />
</bean>

Output

Developer [language=Language [name=Java]]

With autowire by constructor enabled, you can leave the constructor property unset. Spring will find the compatible data type and wire it automatcailly.

	<bean id="developer" class="com.mkyong.common.Developer" autowire="constructor" />

	<bean id="language" class="com.mkyong.common.Language" >
<property name="name" value="Java" />
</bean>

Output

Developer [language=Language [name=Java]]

Spring Autowiring by Constructor的更多相关文章

  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 Auto-Wiring Beans

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

  4. Spring Autowiring by Name

    In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...

  5. Spring Autowiring by Type

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

  6. Spring Autowiring @Qualifier example

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

  7. Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method

    项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...

  8. Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序

    package com.xx; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org. ...

  9. 如何实现一个简易版的 Spring - 如何实现 Constructor 注入

    前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...

随机推荐

  1. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

  2. Wince 中如何实现注册表恢复原厂设置

    理论: 使用HIVE注册表,系统在完成了第一阶段也就是加载完了boot.hv+binfs之后和加载系统HIVE注册表之前,filesys.exe都会调用OEMIoControl来查询是否需要清除保存在 ...

  3. UVa 10892 (GCD) LCM Cardinality

    我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...

  4. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  5. 用I/O口模拟IIC总线协议遇到的一些问题

    最近做的一个项目,是基于IIC总线通信的传感器系统.由于另外一个传感器使用的是类IIC协议,而不是标准IIC,所以MCU不能与其通信,最后没有办法,只有通过I/O口模拟的方式实现IIC的总线通信.具体 ...

  6. Java知识点:javac命令

    javac命令初窥 注:以下红色标记的参数在下文中有所讲解. 用法: javac <options> <source files> 其中, 可能的选项包括:   -g     ...

  7. 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&CCMutableDictionary使用与注意!

    本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/694.html ☞ ...

  8. 【转】IOS 计时器 NSTimer

    原文网址:http://blog.csdn.net/tangshoulin/article/details/7644124 1.初始化 + (NSTimer *)timerWithTimeInterv ...

  9. Windows环境自动获取AWR报告

    1.双击awr.cmd,通过cmd窗口运行awr.sql cmd.exe /c sqlplus lcam_1230/zcpzg1z_1230@54_orcl @awr.sql awr.cmd 2.aw ...

  10. 利用反射自动生成SQL语句(仿Linq)

    转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...