Spring注释驱动配置中的NoUniqueBeanDefinitionException

kar*_*uma 4 spring spring-integration spring-annotations

尝试使用自动装配两个bean时,我收到以下错误

没有定义[javax.jms.ConnectionFactory]类型的限定bean:期望的单个匹配bean但找到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
Run Code Online (Sandbox Code Playgroud)

我有这个注释驱动配置:

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

任何想法在这段代码中有什么问题?这似乎很简单,但是指定限定符不起作用,我也尝试使用@Resource.我在那里错过了什么?

任何帮助赞赏.

Art*_*lan 6

您的代码没有错.

这只是JmsAnnotationDrivenConfiguration来自Spring Boot,它不喜欢你的两个ConnectionFactorybean,但只需要一个.

  1. 为什么只是不遵循该报告的建议并将其中一个标记为@Primary

  2. 看起来您不使用Spring Boot JMS自动配置功能,因此禁用该功能非常简单JmsAnnotationDrivenConfiguration:http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/ #使用启动-禁用-特定自动配置