没有定义类型的唯一bean:期望的单个匹配bean但找到2

use*_*926 7 java spring javabeans

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

 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
Run Code Online (Sandbox Code Playgroud)

我有3个不同的项目,一个是Common,第二个是Adapter,第三个是Service.适配器依赖于Common,而Service依赖于Adapter.这三个都是maven项目.现在在我的Common项目中,我有一个名为CommonAdapter.java的接口

 public interface CommonAdapter {
    List service() ;
}
Run Code Online (Sandbox Code Playgroud)

我在同一个项目中有一个名为AdapterFactory.java的类(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>
Run Code Online (Sandbox Code Playgroud)

现在在我的Adapter Project中,我有CommonAdapter.java的实现类,一个是EndecaAdapetr.java,另一个是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
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在我的Service项目中,想要根据输入调用上面两个类的服务方法.

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

And*_*san 11

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

  1. 只有一个容器管理的bean,IS-A声明了自动装配字段的类型;
  2. 有更多容器管理的bean可以验证上面的IS-A条件,但是自动装配的字段也是合格的(在Spring中,通过使用@Qualifier注释)
  3. 你不使用@Autowired,而是按名称注入bean.

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

endeca IS-A Adapter

solarIS-A Adapter.

因此容器没有唯一的autowire候选者,因此它在设置时崩溃.


Abh*_*yak 8

使用@Primary@Resource当你有多个实现类.

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

@Component("solar")
public class SolarAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}
Run Code Online (Sandbox Code Playgroud)

并注入如下:

@Resource("solar")
Adapter solarAdapter;
Run Code Online (Sandbox Code Playgroud)