NoUniqueBeanDefinitionException in Spring configuration with annotation

I get the following error when trying to auto-arrange two beans using

A bean of type [javax.jms.ConnectionFactory] is not defined: the expected one-time bean match, but 2 found: 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 is wrong with this code? This seems straightforward, but the Qualifier hint is not working, I also tried using @Resource. What am I missing there?

Any help appreciated.

+4
source share
2 answers

There is nothing wrong with the code.

JmsAnnotationDrivenConfiguration Spring Boot, ConnectionFactory beans, .

+5

javax.jms.ConnectionFactory - , , !

:

  • , ConnectionFactory .
  • @Scope ( "singleton" ) @Scope ("prototype").
  • , . @Scope ( "singleton" )
  • " " , .
0

Source: https://habr.com/ru/post/1660104/


All Articles