您的位置:首页 > 编程语言 > Java开发

Spring@Autowired注解与自动装配

2014-01-05 13:37 507 查看

       今天在预发布环境配置一个manager的时候,应用启动失败,报错:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.taobao.deptadmin.biz.core.dynup.DynupSearchManager com.taobao.deptadmin.biz.core.dynup.impl.DefaultDynupRuleBuildManager.dynupSearchManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynupSearchManager': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.taobao.common.searchengine.SearchManager com.taobao.deptadmin.biz.core.dynup.impl.DefaultDynupSearchManager.searchManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.taobao.common.searchengine.SearchManager] is defined: expected single matching bean but found 2: [dynupSpuSearchManager, searchEngineManager]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 152 more

       主要是:

       No unique bean of type [com.taobao.common.searchengine.SearchManager] is defined: expected single matching bean but found 2

      DynupSearchManager依赖了SearchManager,类类型为com.taobao.common.searchengine.DefaultSearchManager,DynupSearchManager使用@autowire 关键字来注入SearchManager属性。由于业务需要,我在另外一个地方也需要配置一个类型为DefaultSearchManager的manager。之前的代码使用@autowire
关键字来自动注入,为类型匹配。如果出现了两个相同的类型,那么会报错,就是日志中的错误。所以这里不呢个使用@autowire 来注入。可以去掉@autowire,用set方法,在xml文件中,用by-name的方式来注入。或者使用@resource来注入,或者使用@Qualifier来做。

 参考资料

使用 Spring 2.5 注释驱动的 IoC 功能
Spring@Autowired注解与自动装配

Spring 3.0 注解注入详解

Spring中@Autowired注解、@Resource注解的区别
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: