具有独特 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 的类 asscoaites 在这里……

 @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(); }

请帮忙!

原文由 Rob McFeely 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 523
2 个回答

问题是因为您有一个 SuggestionService 类型的 bean,它是通过 @Component 注释和 XML config 创建的。正如 JB Nizet 所解释的,这将导致创建一个名为“suggestionService”的 bean,该 bean 通过 @Component 创建,另一个名为“SuggestionService”的 bean 通过 XML 创建。

当您通过 @Autowired 引用 SuggestionService 时,在您的控制器中,Spring 默认“按类型”自动装配并找到两个类型为“SuggestionService”的 bean

您可以执行以下操作之一

  1. 从您的服务中删除 @Component 并依赖于通过 XML 的映射 - 最简单

  2. 从 XML 中删除 SuggestionService 并自动装配依赖项 - 使用 util:map 注入 indexSearchers 映射。

  3. 使用 @Resource 而不是 @Autowired 来按名称挑选 bean。

     @Resource(name="suggestionService")
    private SuggestionService service;

或者

    @Resource(name="SuggestionService")
    private SuggestionService service;

两者都应该工作。第三个是脏修复,最好通过其他方式解决 bean 冲突。

原文由 Aravind A 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您将同一类的 2 个 bean 自动装配到一个类,您应该使用 @QualifierSpring Autowiring @Qualifier 示例)。

但您的问题似乎来自不正确的 Java 语法。

您的对象应以小写字母开头

SuggestionService suggestion;

你的 setter 也应该以小写字母开头,对象名称应该是大写字母

public void setSuggestion(final Suggestion suggestion) {
    this.suggestion = suggestion;
}

原文由 danny.lesnik 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题