Spring Autowiring by Constructor
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的更多相关文章
- 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 Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...
- Spring Autowiring by Name
In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...
- Spring Autowiring by Type
In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...
- Spring Autowiring @Qualifier example
In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario : ...
- Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...
- Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序
package com.xx; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org. ...
- 如何实现一个简易版的 Spring - 如何实现 Constructor 注入
前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...
随机推荐
- Ubuntu 64位系统安装StarUML之最佳实践
preview 相信很多使用Ubuntu的哥们在安装StarUML或者其他软件时都会遇到要求libgcrypt11的依赖.而遗憾的时,这个东西很多人根本找不到. 我将它分享到百度网盘,mark. 一. ...
- BootStrap图标
- LA 3890 (半平面交) Most Distant Point from the Sea
题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个 ...
- hdu2457:DNA repair
AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...
- BZOJ2229: [Zjoi2011]最小割
题解: 真是一道神题!!! 大家还是围观JZP的题解吧(网址找不到了...) 代码: #include<cstdio> #include<cstdlib> #include&l ...
- ASP.NET中MEMCACHED
一,准备 你需要有一下软件: VS.NET(05/08) SQLSERVER memcached服务器端以及客户端类库(开源软件,下载即可)其中,客户 ...
- (转)在mac上配置cocos2d-x开发环境
转自:http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一.下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录 ...
- Linux中 干掉原来的PHP方法
干掉原来的PHP方法: 查看php版本命令:#php -v这个命令是删除不干净的#yum remove php因为使用这个命令以后再用#php -v还是会看到有版本信息的..... 必须强制删除#rp ...
- android和ios流媒体库推荐
1基本信息编辑 Vitamio是一款 Android 与 iOS 平台上的全能多媒体开发框架,全面支持硬件解码与 GPU 渲染.从2011年8月上线到2014年1月,Vitamio 凭借其简洁易用的 ...
- Android 生成含签名文件的apk安装包
做android开发时,必然需要打包生成apk文件,这样才能部署.作为一个完善的apk,必然少不了签名文件,否则下次系统无法进行更新. 一.签名文件的制作及打包生成APK文件 签名文件比较流行的制作方 ...