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. C# Index 定义索---引具体使用2

    窗体代码 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...

  2. 解决 “无法安装 Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持”

    http://blog.csdn.net/davidhsing/article/details/8762621 无法安装Microsoft visual studio 2010 service pac ...

  3. Android开发之EventBus的简单使用

    参考: 1.http://blog.csdn.net/harvic880925/article/details/40660137 2.http://blog.csdn.net/harvic880925 ...

  4. IP地址分类及私网IP

    5类IP地址: IP地址共有32位字节,其中A~C类IP地址由类标识号.网络地址和主机地址组成,A类标识最高位为0,网络地址为1字节,主机地址为3字节, B类标识最高位为10,网络地址为2字节,主机地 ...

  5. 数据库编程与C#编程互译

    今天有一段代码,先是用程序实现. 闲来无聊,又用存储过程实现了一次. 程序中实现. /// <summary> /// 根据区域和用户名获取可访问的国家 /// </summary& ...

  6. HDU 1686 (KMP模式串出现的次数) Oulipo

    题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...

  7. fastdb中的位图应用

    位图内存管理: 每块内存用一个二进制位表示它的使用状态,如果该块内存被占用,则把对应位图中的对应位置1,如果空闲则置0,原理十分简单.计算机里面处理的位数最少的变量是字节(byte),所以也就是8位做 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  10. CSS选择器的兼容性

    CSS 1 CSS2.1 CSS3 :hover 在IE6中只有a元素可用.E:empty 貌似在webkit核心浏览器中有些小bug.如果这个bug依然存在,不太确定如何测试.IE6不支持.clas ...