我正在嘗試使用NoUniqueBeanDefinitionException在Spring註解驅動的配置
類型沒有合格的bean自動裝配兩個bean時,下面的錯誤[javax.jms.ConnectionFactory]定義: 預期單個匹配豆卻發現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。我在那裏錯過了什麼?
任何幫助表示讚賞。
春天混淆和@Bean(name =「aConnectionFactory」)。作爲控制檯建議你需要將它們標記爲主(同bConnectionFactory) – kuhajeyan