尝试使用
自动连接两个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
我有这个注释驱动的配置:
@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;
}
}
任何想法在这段代码中有什么问题?这似乎很简单,但是指定限定符不起作用,我也尝试使用@Resource。我在那里错过了什么?
任何帮助表示赞赏。
答案 0 :(得分:6)
您的代码没有错。
这只是来自Spring Boot的JmsAnnotationDrivenConfiguration
,它不像你的两个ConnectionFactory
bean,但只需要一个。
为什么不遵循该报告建议并使用@Primary
标记其中一个?
您似乎没有使用Spring Boot JMS自动配置功能,因此只需简单地停用JmsAnnotationDrivenConfiguration
:http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration
答案 1 :(得分:0)
问题包括
javax.jms.ConnectionFactory是singleton,你需要一个类型的对象!
您的问题的解决方案: