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. javascript 字符转义汇总

    在开发中经常遇到需要字符转义的,我将一一把遇到的转义列举出来 1.今天中午做项目的时候遇到一个字符串链接的问题,需要链接的的是一个函数的参数 时间字符串:"2014-04-08 16:37: ...

  2. VS2012安装英文的语言包后,调试的时候提示Unknown error:0x80040d10

    https://social.msdn.microsoft.com/Forums/en-US/e11a86ef-3be2-4256-92e9-d12809f2a6ca/error-0x80040d10 ...

  3. js 中 typeof 的使用

    js中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值: 'undefined' --- 这个值未定义 'boolean' -- ...

  4. Android ContentProvider和Uri详解 (绝对全面)

        ContentProvider的基本概念 : 1.ContentProvider为存储和读取数据提供了统一的接口 2.使用ContentProvider,应用程序可以实现数据共享 3.andr ...

  5. 函数fil_io

    /********************************************************************//** Reads or writes data. This ...

  6. 1085: [SCOI2005]骑士精神

    A*搜索. A*搜索的基础百度百科(实在偷懒没看论文之类的),在这里不说了. A*搜索很关键的是h(n)估价函数的选取(表示现在到结束节点需要的距离) 设d(n)为实际到结束节点的距离.h(n)< ...

  7. BZOJ1049: [HAOI2006]数字序列

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题解: ydc的题解:http://pan.baidu.com/share/link?u ...

  8. ti processor sdk linux am335x evm /bin/setup-uboot-env.sh hacking

    #!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-uboot-env.sh hacking # 说明: # 本文主要对TI的sdk中 ...

  9. ubuntu - chrome 标题栏, 书签乱码 解决

    只要修改/etc/fonts/conf.d/49-sansserif.conf这个文件就行了—— 打开/etc/fonts/conf.d/49-sansserif.conf这个文件: sudo ged ...

  10. 最简单的视音频播放示例9:SDL2播放PCM

    本文记录SDL播放音频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...