Spring工厂 - NoUniqueBeanDefinitionException:

时间:2016-02-01 16:53:06

标签: spring

我正在尝试实现工厂模式以从可用的列表中获取生产者。在这样做时,我得到以下例外。无法弄清楚代码的问题。你能告诉我我错过的东西吗?

org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[com.test.interfaces.Producer]类型的限定bean:期望的单个匹配bean但找到2:A,B

请找到以下代码

    public interface Producer<T>  {
    public void start();
    public List<T> produce() throws CEHServiceException;
    public void stop();
}

@Component("A")
public class ProducerA extends Producer  {
    //Autowire Services & Properties

}

@Component("B")
public class ProducerB extends Producer  {
    //Autowire Services & Properties
}

@Configuration
public class AgentConfiguration {
    @Bean
    public ServiceLocatorFactoryBean createProducerFactoryBean(){
        ServiceLocatorFactoryBean bean = new ServiceLocatorFactoryBean();
        bean.setServiceLocatorInterface(ProducerFactory.class);
        return bean;
    }
}

public interface ProducerFactory {

    Producer getProducer(String producerName);

}

@Component
public class AdvancedAgentProcessor {

    @Autowired
    private ObjectFactory<AdvancedRunnerImpl> runnerFactory;
    public void init(){
        AdvancedRunnerImpl runner = runnerFactory.getObject();
        runner.setProducerName("A");
        runner.start();
    }

}


@Component
@Scope("prototype")
public class AdvancedRunnerImpl implements Runner {

    @Autowired private ProducerFactory producerFactory;
    private Producer producer;
    private String producerName;

    public void start() {
        producer = producerFactory.getProducer(this.producerName);
    }

}

Full stack tracke

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.etl.interfaces.Producer] is defined: expected single matching bean but found 2: A,B
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.beans.factory.config.ServiceLocatorFactoryBean$ServiceLocatorInvocationHandler.invokeServiceLocatorMethod(ServiceLocatorFactoryBean.java:377)
    at org.springframework.beans.factory.config.ServiceLocatorFactoryBean$ServiceLocatorInvocationHandler.invoke(ServiceLocatorFactoryBean.java:363)
    at com.sun.proxy.$Proxy34.getProducer(Unknown Source)
    at com.test.runner.AdvancedRunnerImpl.start(AdvancedRunnerImpl.java:54)
    at com.test.app.AdvancedAgentProcessor.init(AdvancedAgentProcessor.java:48)
    at com.test.app.DataAgentApplication.main(DataAgentApplication.java:25)

3 个答案:

答案 0 :(得分:0)

你有两个扩展Producer的bean。在某个地方,你正试图自动生产制作人。 Spring不知道使用哪个Producer。

答案 1 :(得分:0)

Spring不知道自动装配哪个组件。似乎问题出现在ProducerFactoryImplementation中,但我们看不到它。

有两种可能的解决方案:

  1. 使用限定符,以便告诉Spring您需要哪种具体实现。在StackOverflow中有一个示例here
  2. 自动挂载bean列表。类似的东西:

    @Autowired private List<Producer> myAvalilableProducers;
    
    public Producer getByName(name){
    
        for( Producer producer: myAvalilableProducers){
            if(producer.getName().equals(name)){ return producer; }
        }
        throw new RuntimeException("No producer with name " + name " found");
    }
    

答案 2 :(得分:0)

当动态代理无法选择正确的Bean时,就会发生这种情况。请检查this.producerName是否为空或为空。