码迷,mamicode.com
首页 > 编程语言 > 详细

spring依赖注入单元测试:expected single matching bean but found 2

时间:2016-11-16 02:32:09      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:data-   链接   也会   spring   test   href   使用   word   bean   

异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.byd.mes.bussiness.service.siteoperation.activity.ExecuteActivityService] is defined: expected single matching bean but found 2: [executeActivityService, executeActivityForTesterService]

 

解决方案:

博文链接地址:http://haowei0315.javaeye.com/blog/444731

@Qualifier 
@Autowired是根据类型进行自动装配的。在上面的例子中,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。 
1. 可能存在多个UserDao实例

 

[java] view plain copy
 
  1. @Autowired  
  2. public void setUserDao(@Qualifier("userDao") UserDao userDao) {  
  3.     this.userDao = userDao;  
  4. }  

 

这样,Spring会找到id为userDao的bean进行装配。 
2. 可能不存在UserDao实例

 

[java] view plain copy
 
  1. @Autowired(required = false)  
  2. public void setUserDao(UserDao userDao) {  
  3.     this.userDao = userDao;  
  4. }  

  

 http://blog.csdn.net/hjjzhangkui/article/details/6052329

 

spring依赖注入单元测试:expected single matching bean but found 2

标签:data-   链接   也会   spring   test   href   使用   word   bean   

原文地址:http://www.cnblogs.com/softidea/p/6067756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!