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. apk反编译(2)smali语言及文件

    Smali语言是Davlik的虚拟机使用的一种语言,用toolapk反编译apk后,可以见到大量的.smali文件. 可以按照smali语法对其修改,然后重新生成一个未签名的apk. 下面是一个示例: ...

  2. Servlet和JAVA BEAN 分析探讨

    在JSP中调用JAVA类和使用JavaBean有什么区别? 可以像使用一般的类一样使用JavaBean,Bean只是一种特殊的类.特殊在可以通过<jsp:useBean   />调用Jav ...

  3. javascript实现继承的一种方式

    function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototy ...

  4. android和ios流媒体库推荐

    1基本信息编辑 Vitamio是一款 Android 与 iOS 平台上的全能多媒体开发框架,全面支持硬件解码与 GPU 渲染.从2011年8月上线到2014年1月,Vitamio 凭借其简洁易用的 ...

  5. 深入学习Oracle分区表及分区索引

    关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: •       Range(范围)分区 •       Has ...

  6. 多线程程序设计学习(5)balking模式和timed模式

    Balking[返回模式]timed[超时模式]一:balking pattern的参与者--->GuardedObject(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...

  7. Oracle 跟踪事件 set event

    一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...

  8. .NET/ASP.NET Routing路由(深入解析路由系统架构原理)http://wangqingpei557.blog.51cto.com/1009349/1312422

    阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...

  9. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8

    [问题] 折腾: [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 过程中,增大对应AVD的内存为2G后,结果无法启 ...

  10. Xcode中使用svn时,报证书验证错误Error validating server certificate for

    转:http://blog.csdn.net/yhawaii/article/details/7511141 今天使用Xcode自带的svn客户端时,总是连接不上服务器,报如下错误: Error va ...