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

  1. Spring Auto-Wiring Beans

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

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

  3. Spring Autowiring by Constructor

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

  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 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. Python转义字符

    在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.有时我们并不想让转义字符生效,我们只想显示字符串原来的意思,这就要用r和R来定义原始字符串.如:print r'\t\r'实际输出为“ ...

  2. bzoj2788

    明显是一个差分约束系统 对于第一种限制,其实就是x[a]+1<=x[b] x[b]-1<=x[a] 根据三角不等式很容易建图 但这题他比较奇怪,问的是X最多不同取值的个数 根据这张图的特殊 ...

  3. UVa 12716 (GCD == XOR) GCD XOR

    题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...

  4. Can't dispatch DDM chunk 52454151: no handler defined

    [2010-07-12 10:10:06 - Hello Google Android]ActivityManager: DDM dispatch reg wait timeout [2010-07- ...

  5. iDSDT搞定显卡和声卡 黑苹果不纠结

    原帖:http://www.lovelucy.info/idsdt-mac-video-audio-drive.html 之前写过PC机上装Mac OS X系统,准备工作中最纠结的就是驱动了.在网络上 ...

  6. buildroot linux filesystem 初探

    /****************************************************************************** * buildroot linux fi ...

  7. HDU 5313 Bipartite Graph (二分图着色,dp)

    题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...

  8. JavaScript学习笔记(备忘录)

    ===运算符 判断数值和类型是否相等.如: console.log('s'==='s') //输出trueconsole.log('1'===1) //输出false

  9. Android-onTouchEvent方法的使用

    手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触摸事件.该方法的签名如下所示. public ...

  10. OracleBulkCopy的批量数据导入

    private void button1_Click(object sender, EventArgs e) { OpenFileDialog afd = new OpenFileDialog(); ...