Given that Spring can autowire either by
type or by
name, it is always confusing to me that what is the default behavior when using annocation @Autowired or @Resource to do the autowire.
Therefore, I made an experiment to help clear out confusion. Here are some of my findings:






1. By default @Autowired and @Resource will first use field name or parameter
name to autowire bean with the same name as bean id. If it can't find any matching bean id, then it will autowire by type.
2. When autowire by type, for certain type, there must be only one bean existing in the application context. Otherwise, Spring will throw exception like: No
unique bean of type [com.ms.junz.domain.User] is defined: expected single matching bean but found 2: [user1, user]
3. If your field name is independent of bean id and there might be multiple beans with the same type, you can use either@Qualifier("beanId") together
with @Autowired, or @Resource(name="beanId") to
explicitly tell Spring which bean to autowire. Then, Spring wouldn't complain any longer.
本文探讨了Spring框架中@Autowired和@Resource注解的默认行为,并通过实验验证了它们如何按名称或类型进行自动装配。此外,还介绍了如何使用@Qualifier解决类型匹配不唯一的问题。
930

被折叠的 条评论
为什么被折叠?



