使用唯一的bean进行spring自动装配:spring需要单个匹配bean,但是发现2
问题内容:
我正在尝试使用Spring为webapp自动装配一些bean(用于依赖注入)。一个控制器bean包含另一个bean,后者又保存另一组bean的哈希图。目前,该地图只有一个条目。当我在tomcat中运行并调用服务时,我收到一条错误消息,说第二个bean(保存在控制器中)不是唯一的
No unique bean of type [com.hp.it.km.search.web.suggestion.SuggestionService] is defined: expected single matching bean but found 2: [suggestionService, SuggestionService]
我看不到我在两次定义bean的地方,但是这是Spring的新知识,并且是自动装配的,因此我可能缺少一些基本知识。下面列出的xml和2类的源代码…
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hp.it.km.search.web.suggestion" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="SuggestionController" class="com.hp.it.km.search.web.suggestion.SuggestionController">
<property name="service">
<ref bean="SuggestionService" />
</property>
</bean>
<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
<property name="indexSearchers">
<map>
<entry key="KMSearcher"> <ref bean="KMSearcherBean"></ref></entry>
</map>
</property>
</bean>
<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
<constructor-arg index="0" value="KMSearcher" />
<constructor-arg index="1" value="C://dev//workspace//search-restful-webapp//src//main//resources//indexes//keyword" />
</bean>
自动接线控制器和服务Bean的类asscoaite在这里…
@Controller
public class SuggestionController {
private SuggestionService service;
@Autowired
public void setService(SuggestionService service) {
this.service = service;
}
public SuggestionService getService() {
return service;
}
和…
@Component
public class SuggestionService {
private Map<String, IndexSearcher> indexSearchers = new HashMap<String, IndexSearcher>();
@Autowired
public void setIndexSearchers(Map<String, IndexSearcher> indexSearchers) {
this.indexSearchers = indexSearchers;
}
public SuggestionService() {
super(); }
请帮忙!
问题答案:
问题是因为您有一个通过@Component注释以及通过XML config创建的类型为RecommendationionService的bean。正如JB
Nizet解释的那样,这将导致创建一个通过@Component创建的名称为’suggestionService’的bean和另一个通过XML创建的名称为’SuggestionService’的bean。
当您通过@Autowired引用RecommendationionService时,默认情况下,在控制器中,Spring自动按类型“按类型”自动连接并找到两个类型为“
SuggestionService”的bean
您可以执行以下操作
- 从您的服务中删除@Component并依赖于通过XML映射-最简单
- 从XML移除RecommendationionService并自动关联依赖项-使用util:map注入indexSearchers映射。
- 使用@Resource而不是@Autowired来按其名称选择bean。
@Resource("suggestionService")
private SuggestionService service;
要么
@Resource("SuggestionService")
private SuggestionService service;
两者都应该起作用。第三是肮脏的修复程序,最好通过其他方式解决bean冲突。