Spring Autowiring by Type
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的更多相关文章
- 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 AutoDetect
In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...
- Spring Autowiring by Constructor
In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...
- Spring Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...
- 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 @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 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 ...
随机推荐
- xmlsechema验证
//创建xmlDocument XmlDocument doc = new XmlDocument(); ...
- hdu 4968 Improving the GPA (水 暴力枚举)
题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10, ...
- POJ 2516 最小费用流
依然最小费用最大流模板题 建边麻烦了些 #include <cstdio> #include <cstring> #include <iostream> #incl ...
- Apache Struts2 s2-020补丁安全绕过漏洞
CNVD-ID CNVD-2014-01552 发布时间 2014-03-11 危害级别 高 影响产品 Apache struts 2.0.0-2.3.16 BUGTRAQ ID 65999 CVE ...
- javascript留言板
用DOM相关方法创建的留言板 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> ...
- 浏览器HTML5支持程度测试
/********************************************************************* * 浏览器HTML5支持程度测试 * 说明: * 想知道对 ...
- UVA 820 Internet Bandwidth 因特网宽带(无向图,最大流,常规)
题意:给一个无向图,每条边上都有容量的限制,要求求出给定起点和终点的最大流. 思路:每条无向边就得拆成2条,每条还得有反向边,所以共4条.源点汇点已经给出,所以不用建了.直接在图上跑最大流就可以了. ...
- oracle中怎么查看存储过程的源码
今天想找几天前写的存储过程的源码看看,发现自己熟悉的命令怎么都不好使,要不提示标示符错误要不就是提示未选定行,通过baidu得知type跟name变量要弄成大写的.. select text from ...
- AngularJS:实现动态添加输入控件功能(转)
http://www.cnblogs.com/ilovewindy/p/3849428.html <!DOCTYPE html> <html> <head> < ...
- Android-AnimationDrawable(二)
首先可以先定义一个逐帧播放的xml: <?xml version="1.0" encoding="utf-8"?> <animation-li ...