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.

最新文章

  1. Hexo主题实现多级分类显示
  2. CoffeeScript的类继承的工具函数extends
  3. ios批量打包
  4. [[4], [5, 6, 7]](Python)list的方法
  5. 获取网页上数据(图片、文字、视频)-b
  6. JPA事务回滚配置
  7. MVC 4.0项目部署在IIS上无法浏览的解决方案
  8. Linux部署ASP.NET 5 (vNext)
  9. 获取synchronized锁中的阻塞队列中的线程是非公平的
  10. 自动清理SQLServerErrorLog错误日志避免太大
  11. 201521123010 《Java程序设计》第11周学习总结
  12. 14 fragment 创建
  13. rsync用法详细解释
  14. Android adb 串口调试
  15. java中的日志打印
  16. python 保存小数位,控制保存几位
  17. 20165236 2017-2018-2 《Java程序设计》第八周学习总结
  18. CF258D Little Elephant and Broken Sorting (带技巧的DP)
  19. HTML表单组件
  20. ASP Session的功能的缺陷(进程外的Session)

热门文章

  1. const和readonly的区别
  2. [Codeforces676B]Pyramid of Glasses(递推,DP)
  3. POJ2796 单调队列
  4. bzoj4154
  5. 页面多个Jquery版本共存的冲突问题,解决方法!
  6. Android 开发问题集合
  7. 如何在Android应用中加入广告
  8. 【C#学习笔记】获取当前应用程序所在路径及环境变量
  9. iOS - GIF图的完美拆解、合成、显示
  10. ViewPager设置 缓存个数、页卡间距、数据更新