Spring 注释驱动配置中的 NoUniqueBeanDefinitionException
NoUniqueBeanDefinitionException in Spring annotation driven configuration
尝试使用
自动装配两个 bean 时出现以下错误
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
我有这个注解驱动的配置:
@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。我在那里错过了什么?
感谢任何帮助。
问题在于
javax.jms.ConnectionFactory是单例,需要一个对象!
您的问题的解决方案:
- 如果您需要两个创建对象和扩展 ConnectionFactory 的对象
他们根据需要更改范围。
- 试试@Scope("singleton") 或@Scope("prototype").
- 如果您收到错误,请创建一个对象。然后使用范围 @Scope("singleton")
- "Other Two" 毁容另一个已经在使用和设置这样的 class。
你的代码没有问题。
那只是 JmsAnnotationDrivenConfiguration
来自 Spring Boot,它不喜欢你的两个 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
尝试使用
自动装配两个 bean 时出现以下错误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
我有这个注解驱动的配置:
@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。我在那里错过了什么?
感谢任何帮助。
问题在于
javax.jms.ConnectionFactory是单例,需要一个对象!
您的问题的解决方案:
- 如果您需要两个创建对象和扩展 ConnectionFactory 的对象 他们根据需要更改范围。
- 试试@Scope("singleton") 或@Scope("prototype").
- 如果您收到错误,请创建一个对象。然后使用范围 @Scope("singleton")
- "Other Two" 毁容另一个已经在使用和设置这样的 class。
你的代码没有问题。
那只是 JmsAnnotationDrivenConfiguration
来自 Spring Boot,它不喜欢你的两个 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