Spring Autowiring by AutoDetect
In Spring, “Autowiring by AutoDetect“, means chooses “autowire by constructor” if default constructor (argument with any data type), otherwise uses “autowire by type“.
See an example of Spring “auto wiring by autodetect”. Auto wiring the “kungfu
” bean into “panda
”, via constructor or type (base on the implementation of panda
bean).
<bean id="panda" class="com.mkyong.common.Panda" autowire="autodetect" />
<bean id="kungfu" class="com.mkyong.common.KungFu" >
<property name="name" value="Shao lin" />
</bean>
1. AutoDetect – by Constructor
If a default constructor is supplied, auto detect will chooses wire by constructor.
package com.mkyong.common;
public class Panda {
private KungFu kungfu;
public Panda(KungFu kungfu) {
System.out.println("autowiring by constructor");
this.kungfu = kungfu;
}
public KungFu getKungfu() {
return kungfu;
}
public void setKungfu(KungFu kungfu) {
System.out.println("autowiring by type");
this.kungfu = kungfu;
}
//...
}
Output
autowiring by constructor
Person [kungfu=Language [name=Shao lin]]
2. AutoDetect – by Type
If a default constructor is not found, auto detect will chooses wire by type.
package com.mkyong.common;
public class Panda {
private KungFu kungfu;
public KungFu getKungfu() {
return kungfu;
}
public void setKungfu(KungFu kungfu) {
System.out.println("autowiring by type");
this.kungfu = kungfu;
}
//...
}
Output
autowiring by type
Person [kungfu=Language [name=Shao lin]]
Spring Autowiring by AutoDetect的更多相关文章
- Spring Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...
- 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 Constructor
In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...
- 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 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 ...
随机推荐
- 在win7系统下使用TortoiseGit(乌龟git)简单操作Git@OSC
非常感谢OSC提供了这么好的一个国内的免费的git托管平台.这里简单说下TortoiseGit操作的流程.很傻瓜了 首先你要准备两个软件,分别是msysgit和tortoisegit,乌龟还可以在下载 ...
- JazzyViewPager开源项目的简析及使用
JazzyViewPager是一个重写的ViewPager,能是ViewPager滑动起来更加的炫酷. 开源地址:https://github.com/jfeinstein10/JazzyViewPa ...
- <二>面向对象分析之几个关键的概念
一:建模 --->建模,是指通过对[客观事物]建立一种抽象的方法用以表征事物并获得对事物本身的理解.同时把这种理解概念化,将这些逻辑概念组织起来,构成一种对所观察对象的内部结构和工 ...
- WPF如何在同一个区域依次叠加显示多张图片呢?
正如标题的问题,有时需要在已显示的图片的右上角(或其他区域)显示小图标,譬如下图的患者头像右上角显示病情图标:(这里不采用事先用PS编排成一个图片文件的方式,因为此方式普适性不好) 解决方案:绘制该复 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- android获取手机信息2
IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...
- POJ 3321- Apple Tree(标号+BIT)
题意: 给你一棵树,初始各节点有一个苹果,给出两种操作,C x 表示若x节点有苹果拿掉,无苹果就长一个. Q x查询以x为根的子树中有多少个苹果. 分析: 开始这个题无从下手,祖先由孩子的标号不能确定 ...
- HDU 4325-Flowers(线段树+离散化)
题意: 给出每个花开花的时间段,每询问一个时间点输出该时间点开花的数量 分析: 线段树的区间更新,单点查询,但发现时间很大,没法存区间,就想到了离散化. 离散化就是把要处理的数据统一起来重新标号. # ...
- CSS中样式覆盖优先顺序
原文地址:http://www.3lian.com/edu/2014/09-25/168393.html 层叠优先级是: 浏览器缺省 < 外部样式表 < 内部样式表 < 内联样式 其 ...
- 《Python核心编程》 第十章 错误和异常
10–1. 引发异常. 以下的哪个因素会在程序执行时引发异常? 注意这里我们问的并不是异常的原因. a) 用户 b) 解释器 c) 程序 d) 以上所有 e) 只有 b) 和 c) f) 只有 a) ...