未定义任何类型的唯一Bean:预期为单个匹配的Bean,但找到2 [英] No unique bean of type is defined: expected single matching bean but found 2

查看:68
本文介绍了未定义任何类型的唯一Bean:预期为单个匹配的Bean,但找到2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部署代码时遇到以下异常

I am getting the below exception when I am deploying the code

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.belk.api.adapter.contract.Adapter] is defined: expected single matching bean but found 2: [endeca, solar]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 64 more

我有3个不同的项目,一个是Common,第二是Adapter,第三个是Service. 适配器取决于公共",而服务则取决于适配器".这三个都是Maven项目. 现在在我的Common项目中,我有一个名为CommonAdapter.java

I have 3 different projects one is Common, second is Adapter and the third is Service. The adapter is dependent on Common and Service is dependent on Adapter. All three are maven projects. Now in my Common project I have an interface called CommonAdapter.java

 public interface CommonAdapter {
    List service() ;
}

我在同一项目(即Common)中有一个名为AdapterFactory.java的类

I have a class called AdapterFactory.java in the same project (i,e Common)

@Component
public class AdapterFactory {
    @Autowired
    Adapter adapter;
    public Adapter getAdapter(String adapterName){
        return adapter;
    }

}   
    <context:component-scan base-package="com.test.api" />
    <bean
        class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"
        id="adapterFactory">
        <property name="serviceLocatorInterface" value="com.test.api.adapter.manager.AdapterFactory">
        </property>
    </bean>

现在在我的适配器项目中,我具有CommonAdapter.java的实现类 一个是EndecaAdapetr.java,另一个是SolarAdapter.java

Now in my Adapter Project, I have the implementation classes for CommonAdapter.java One is EndecaAdapetr.java and the other is SolarAdapter.java

@Component("endeca")
public class EndecaAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

@Component("solar")
public class SolarAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

现在在我的Service项目中,要基于输入调用以上两个类的service方法.

Now in my Service project, want to invoke the service method of the above two classes based on the input.

public class ProductSearchServiceImpl {
    @Autowired
    private AdapterFactory adapterFactory;
    public Search searchProducts(){
    Adapter endecaAdapter = this.adapterFactory
                .getAdapter("endeca ");
 }
 }

推荐答案

@Autowired仅在毫无疑问应该注入哪个容器管理实例的情况下有效.基本上,这可以通过(至少)三种方式来实现:

@Autowired works only when there is no doubt on which container-managed instance should be injected. Basically, this can be attained in (at least) 3 ways:

  1. 只有一个容器管理的IS-A声明了自动装配字段的类型;
  2. 还有更多的容器管理bean可以验证上述IS-A条件,但是自动装配字段也是合格的(在Spring中,使用@Qualifier批注)
  3. 您不用@Autowired,而是按名称注入bean.
  1. There is only one container-managed bean that IS-A declared type of the autowired field;
  2. There are more container-managed beans that validate the above IS-A condition, but the autowired field is also qualified (in Spring, by using a @Qualifier annotation)
  3. You don't use @Autowired, but rather inject the beans by name.

在您的情况下,您有两个可以验证IS-A条件的bean:

In your case, you have two beans that validate the IS-A condition:

endeca IS-A Adapter

solar IS-A Adapter.

因此,容器没有唯一的自动装配候选者,因此在设置自身时会崩溃.

So the container does not have a unique autowire candidate, therefore it crashes while setting up itself.

这篇关于未定义任何类型的唯一Bean:预期为单个匹配的Bean,但找到2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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