Search code examples
springspring-integrationspring-annotations

NoUniqueBeanDefinitionException in Spring annotation driven configuration


I am getting the following error when trying to autowire two beans using

No qualifying bean of type [javax.jms.ConnectionFactory] is defined: expected single matching bean but found 2: aConnectionFactory, bConnectionFactory

Description:

Parameter 1 of method jmsListenerContainerFactory in org.springframework.boot.autoconfigure.jms.JmsAnnotationDrivenConfiguration required a single bean, but 2 were found:
        - aConnectionFactory: defined by method 'aConnectionFactory' in package.Application
        - bConnectionFactory: defined by method 'bConnectionFactory' in package.Application


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

I have this annotation driven configuration:

@SpringBootApplication
@EnableIntegration
@IntegrationComponentScan
public class Application  extends SpringBootServletInitializer implements
 WebApplicationInitializer {

    @Resource(name = "aConnectionFactory")
    private ConnectionFactory aConnectionFactory;

    @Resource(name = "bConnectionFactory")
    private ConnectionFactory bConnectionFactory;

    @Bean
    public IntegrationFlow jmsInboundFlow() {
        return IntegrationFlows
                    .from(
                        Jms.inboundAdapter(aConnectionFactory)
                                            .destination(aQueue),
                        e -> e.poller( Pollers.fixedRate(100, 
TimeUnit.MILLISECONDS).maxMessagesPerPoll(100))
                     ).channel("entrypoint")
                     .get();
}

   @Bean
    public IntegrationFlow jmsInboundFlowB() {
        return IntegrationFlows
                    .from(
                        Jms.inboundAdapter(bConnectionFactory)
                                            .destination(bQueue),
                        e -> e.poller( Pollers.fixedRate(100, 
TimeUnit.MILLISECONDS).maxMessagesPerPoll(100))
                     ).channel("entrypoint")
                     .get();
}


    @Bean(name = "aConnectionFactory")
    @Profile({"weblogic"})
    public ConnectionFactory aConnectionFactory() {
        ConnectionFactory factory = null;
        JndiTemplate jndi = new JndiTemplate();
        try {
          factory = (ConnectionFactory) jndi.lookup("jms/ConnectionFactory");
        } catch (NamingException e) {
            logger.error("NamingException for jms/ConnectionFactory", e);
        }

        return factory;
    }

    @Bean(name = "bConnectionFactory")
    @Profile({"weblogic"})
    public ConnectionFactory bConnectionFactory() {
        ConnectionFactory factory = null;
        JndiTemplate jndi = new JndiTemplate();
        try {
          factory = (ConnectionFactory) jndi.lookup("jms/ConnectionFactory");
        } catch (NamingException e) {
            logger.error("NamingException for jms/ConnectionFactory", e);
        }

        return factory;
    }

}

Any ideas what's wrong in this code? This seems to be straight forward, but specifying the Qualifier doesn't work, I have also tried to use @Resource. What am I missing there?

Any help appreciated.


Solution

  • Nothing wrong with your code.

    That is just JmsAnnotationDrivenConfiguration from Spring Boot which doesn't like your two ConnectionFactory beans, but requires only one.

    1. Why just don't follow with that report recommendations and mark one of them with the @Primary?

    2. Looks like you don't use Spring Boot JMS auto-configuration feature, so that would be just straightforward to disable JmsAnnotationDrivenConfiguration: http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration