spring 使用独特的 bean 自动装配:Spring 预计单个匹配 bean 但发现 2 [英] spring autowiring with unique beans: Spring expected single matching bean but found 2

查看:32
本文介绍了spring 使用独特的 bean 自动装配:Spring 预计单个匹配 bean 但发现 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring 为 webapp 自动装配一些 bean(用于依赖注入).一个控制器 bean 包含另一个 bean,该 bean 又包含另一组 bean 的哈希图.目前地图只有一个条目.当我在 tomcat 中运行并调用该服务时,我收到一条错误消息,指出第二个 bean(保存在控制器中)不是唯一的

I am trying to autowire some beans (for dependency injection) using Spring for a webapp. One controller bean contains another bean which in turn holds a hashmap of another set of beans. For now the map only has one entry. When i run in tomcat and call the service I get an error saying that the second bean (held in the controller) is not unique

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 个类的源代码...

I cannot see where I am defining the bean twice however am new to Spring and autowiring so I may be missing something fundamental. Source code for xml and 2 class listed below...

<?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 的类在此处...

The class asscoaites with the autowired controller and service bean are here...

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

请帮忙!

推荐答案

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

The issue is because you have a bean of type SuggestionService created through @Component annotation and also through the XML config . As explained by JB Nizet, this will lead to the creation of a bean with name 'suggestionService' created via @Component and another with name 'SuggestionService' created through XML .

当您通过@Autowired 引用 SuggestionService 时,在您的控制器中,Spring 会按类型"自动装配默认情况下,找到两个 'SuggestionService' 类型的 bean

When you refer SuggestionService by @Autowired, in your controller, Spring autowires "by type" by default and find two beans of type 'SuggestionService'

您可以执行以下操作

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

  1. Remove @Component from your Service and depend on mapping via XML - Easiest

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

Remove SuggestionService from XML and autowire the dependencies - use util:map to inject the indexSearchers map.

使用@Resource 而不是 @Autowired 来按名称选择 bean.

Use @Resource instead of @Autowired to pick the bean by its name .

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

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

两者都应该有效.第三个是一个肮脏的修复,最好通过其他方式解决 bean 冲突.

both should work.The third is a dirty fix and it's best to resolve the bean conflict through other ways.

这篇关于spring 使用独特的 bean 自动装配:Spring 预计单个匹配 bean 但发现 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆