In Spring, “Autowiring by Type” means, if data type of a bean is compatible with the data type of other bean property, auto wire it.

For example, a “person” bean exposes a property with data type of “ability” class, Spring will find the bean with same data type of class “ability” and wire it automatically. And if no matching found, just do nothing.

You can enable this feature via autowire="byType" like below :

	<!-- person has a property type of class "ability" -->
<bean id="person" class="com.mkyong.common.Person" autowire="byType" /> <bean id="invisible" class="com.mkyong.common.Ability" >
<property name="skill" value="Invisible" />
</bean>

See a full example of Spring auto wiring by type.

1. Beans

Two beans, person and ability.

package com.mkyong.common;

public class Person
{
private Ability ability;
//...
}
package com.mkyong.common;

public class Ability
{
private String skill;
//...
}

2. Spring Wiring

Normally, you wire the bean explicitly :

	<bean id="person" class="com.mkyong.common.Person">
<property name="ability" ref="invisible" />
</bean> <bean id="invisible" class="com.mkyong.common.Ability" >
<property name="skill" value="Invisible" />
</bean>

Output

Person [ability=Ability [skill=Invisible]]

With autowire by type enabled, you can leave the ability property unset. Spring will find the same data type and wire it automatcailly.

	<bean id="person" class="com.mkyong.common.Person" autowire="byType" />

	<bean id="invisible" class="com.mkyong.common.Ability" >
<property name="skill" value="Invisible" />
</bean>

Output

Person [ability=Ability [skill=Invisible]]

Wait, what if you have two beans with same data type of class “ability”?

	<bean id="person" class="com.mkyong.common.Person" autowire="byType" />

	<bean id="steal" class="com.mkyong.common.Ability" >
<property name="skill" value="Steal" />
</bean> <bean id="invisible" class="com.mkyong.common.Ability" >
<property name="skill" value="Invisible" />
</bean>

Output

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
...
No unique bean of type [com.mkyong.common.Ability] is defined:
expected single matching bean but found 2: [steal, invisible]; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [com.mkyong.common.Ability] is defined:
expected single matching bean but found 2: [steal, invisible]

In this case, you will hits the UnsatisfiedDependencyException error message.

Note

In autowiring by type mode, you just have to make sure only one unique data type of bean is declared.

Spring Autowiring by Type的更多相关文章

  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 Autowiring by Constructor

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

  4. Spring Auto-Wiring Beans

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

  5. Spring Autowiring by Name

    In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...

  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 Boot:怎样排查 Cannot determine embedded database driver class for database type NONE

    ref:https://www.journaldev.com/13830/spring-boot-cannot-determine-embedded-database-driver-class-for ...

随机推荐

  1. Oracle数据库ORA-12154: TNS: 无法解析指定的连接标识符详解

    ORA-12154: TNS: 无法解析指定的连接标识符(转自http://www.cnblogs.com/psforever/p/3929064.html) 相信使用过Oracle数据库的人一定碰到 ...

  2. GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别2-------C#检查键盘大小写锁定状态

    1.命名空间:using System.Runtime.InteropServices;2.导入方法[DllImport("user32.dll", EntryPoint = &q ...

  3. iOS学习笔记: 使用CAShapeLayer创建带有空心区域的遮罩层

    CAShapeLayer是用来接受矢量Path,直接使用GPU来进行渲染的特殊图层.看下面效果: 对应代码: let markLayer = CAShapeLayer(); markLayer.fra ...

  4. Android log日志

    LOG是用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用. androidsdk中提供了l ...

  5. bzoj1486: [HNOI2009]最小圈

    二分+dfs. 这道题求图的最小环的每条边的权值的平均值μ. 这个平均值是大有用处的,求它我们就不用记录这条环到底有几条边构成. 如果我们把这个图的所有边的权值减去μ,就会出现负环. 所以二分求解. ...

  6. 五大主流SQL数据库

    一. 开放性 1. SQL Server 只能在windows上运行,没有丝毫的开放性,操作系统的系统的稳定对数据库是十分重要的.Windows9X系列产品是偏重于桌面应用,NT server只适合中 ...

  7. 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)

    本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-c ...

  8. wave文件(*.wav)格式、PCM数据格式

    1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...

  9. Spring各jar包的作用(转载)

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  10. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...