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 ...
随机推荐
- ie下jquery ajax 80020101错误的解决方法
<script language="javascript"> <!-- function checkAll(name,isCheck){ ...
- 11月下旬poj其他题
poj1000,poj1003,poj1004,poj1064,poj1218 水题 poj1012:0<k<14——漂亮的打表 poj1651:与能量项链很像的dp poj1159:回文 ...
- 获取本机外网ip和内网ip
获取本机外网ip //获取本机的公网IP public static string GetIP() { string tempip = ""; try { WebRequest r ...
- 通过CSS禁止Chrome自动为输入框添加橘黄色边框,修改/禁止 chrome input边框颜色,
1 /*Chrome浏览器 点击input 黄色边框 禁用*/ .NoOutLine:focus{outline: none} <asp:TextBox ID="txtTeleph ...
- codevs 1137 计算系数
什么时候NOIP也要出二项式定理了? 二项式定理+逆元. #include<iostream> #include<cstdio> #include<cstring> ...
- ffmpeg+rtsp+dss
1. push stream to dss ffmpeg -f mpegts -re -i film.v -c:v libx264 -s 352x288 -aspect 4:3 -b:v 300k - ...
- ActionBarSherlock的学习笔记(一) -------------- ActionBarSherlock的简要介绍
1. 介绍 ActionBarSherlock 是Android compatibility library 的一个扩展, 不知道什么原因 Android 兼容开发包没有包含ActionBar. 所以 ...
- 移动开发之浅析cocos2d-x的中文支持问题
题记:这阵子一直在学习cocos2d-x,其跨平台的特性确实让人舒爽,引擎的框架概念也很成熟,虽然相应的第三方工具略显单薄,但也无愧是一件移动开发的利器啊,有兴趣的朋友有时间就多了解吧. 使用引擎的过 ...
- Ruby 文件处理
#r read, #w write, #a append, #r+ 读写方式 从文件的头位置开始读取或写入, #w+ 读写方式,如果文件已存在清空该文件,不存在就创建一个新的文件, #a+ 如果文件存 ...
- iE6、7、8、9、10、11兼容的Cookie
<%@ Master Language="C#" Debug="true" AutoEventWireup="true" Inheri ...