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 ...
随机推荐
- ubuntu10.04开启root登陆
半年没有用ubuntu了,以前用的是8.10,现在装了一个10.04,第一印象就是登陆窗口变了,哎,比较喜欢用root用户登录系统,不喜欢非root用户,做任何事都要来一下sudo,10.04的登陆窗 ...
- C++STL之整理算法
这里主要介绍颠倒.旋转.随机排列和分类4中常见的整理算法 1.颠倒(反转) void reverse(_BidIt _First, _BidIt _Last) _OutIt reverse_copy( ...
- JVM参数配置
JVM参数配置 设置堆大小 -Xms 初始堆大小 -Xmx 最大堆大小 -Xmn 设置年轻代大小 设置每个线程堆栈大小 -Xss 设置每个线程的堆栈大小 设置年轻代大小 -XX:NewSize= -X ...
- 面试题_66_to_75_Java IO 和 NIO 的面试题
IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...
- bundle update: env: ruby_executable_hooks: No such file or directory
please open a bug here: https://github.com/mpapis/executable-hooks/issues as a temporary fix try: rv ...
- HDU 1166 敌兵布阵 (线段树 单点更新)
题目链接 线段树掌握的很差,打算从头从最简单的开始刷一波, 嗯..就从这个题开始吧! #include <iostream> #include <cstdio> #includ ...
- BZOJ2626: JZPFAR
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2626 题解:裸K-Dtree,最大值?自己yy一下估价函数就好了. 两题居然是同一个错误,真是 ...
- android-async-http
安装 http://blog.csdn.net/wangwei_cq/article/details/9453345 包内的一些基本的参数 http://www.cnblogs.com/manuose ...
- Java Web编程的主要组件技术——MVC设计模式
参考书籍:<J2EE开源编程精要15讲> MVC(Model View Controller),Model(模型)表示业务逻辑层,View(视图)代表表述层,Controller(控制)表 ...
- cgroup的测试数据
[root@xxxx /cgroup/memory/rule3021]#cat memory.limit_in_bytes503316480 480M [root@xxxx /cgroup/mem ...