接口有多个实现下,配置spring默认注入的对象

Spring MVC(4.0)使用@Autowired自动注入bean的时候,异常信息:

 1 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.persistence.EntityManager com.newpp.core.base.dao.BaseJdbcImpl.em; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] is defined: expected single matching bean but found 3: entityManager,org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
 2  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
 3  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
 4  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
 5  ... 21 more
 6 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] is defined: expected single matching bean but found 3: entityManager,org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
 7  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970)
 8  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
 9  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
10  ... 23 more

 

原因:

待注入的接口有多个实现

 

解决办法:

自动注入的时候指定应该被注入的对象

例:

    @Autowired
    @Qualifier("entityManager")
    private EntityManager em;  

posted @ 2014-07-23 17:19  havery  阅读(1530)  评论(0)    收藏  举报