现在的位置: 首页 > 综合 > 正文

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

2013年05月27日 ⁄ 综合 ⁄ 共 834字 ⁄ 字号 评论关闭

异常信息: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实例

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

  

抱歉!评论已关闭.