具有独特的豆春季自动装配:春季预期单个匹配的bean,但发现2 [英] spring autowiring with unique beans: Spring expected single matching bean but found 2

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

问题描述

我想使用Spring的web应用程序自动装配一些豆类(依赖注入)。一个控制器bean包含另一个bean则持有另一套豆的HashMap中。对于现在的地图只有一个入口。当我在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]

我不能看到我的定义豆但是两次是新的春天和自动装配,所以我可能会丢失一些基本的东西。来源$ C ​​$ C对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类asscoaites在这里...

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

请帮助!

推荐答案

的问题是,因为你必须通过@Component注解并且还可以通过XML配置创建的类型SuggestionService的豆。正如JB Nizet解释说,这将导致创建名为'suggestionService通过@Component创建一个bean,而另一个名为'SuggestionService通过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,在你的控制器,春autowires按类型在默认情况下,找到类型的两个豆SuggestionService

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依赖映射 - 最简单

  2. 从XML删除SuggestionService和自动装配的依赖关系 - 使用UTIL:地图注入indexSearchers地图

  3. 使用@Resource而不是@Autowired来接它的名字豆。

  1. Remove @Component from your Service and depend on mapping via XML - Easiest
  2. Remove SuggestionService from XML and autowire the dependencies - use util:map to inject the indexSearchers map.
  3. Use @Resource instead of @Autowired to pick the bean by its name .

@Resource("suggestionService")
private SuggestionService service;


    @Resource("SuggestionService")
    private SuggestionService service;

两者都应该work.The第三个是一个肮脏的修复,这是最好的解决通过其他途径豆冲突。

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

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

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