Java Spring配置(基于注释):NonuniqueBeandDefinitionException:没有类型为'的限定bean;jms.ConnectionFactory';提供:
spring配置:尝试实例化JMSMessageListenerContainerFactorybean,该Bean将使用注释来使用特定的连接工厂Bean实例Java Spring配置(基于注释):NonuniqueBeandDefinitionException:没有类型为'的限定bean;jms.ConnectionFactory';提供:,java,spring,Java,Spring,spring配置:尝试实例化JMSMessageListenerContainerFactorybean,该Bean将使用注释来使用特定的连接工厂Bean实例 @Configuration public class DemoJmsConfig { @Value("${jms.context.factory}") private String initialContextFactory; @Value("${jms.provider.url}") Strin
@Configuration
public class DemoJmsConfig {
@Value("${jms.context.factory}")
private String initialContextFactory;
@Value("${jms.provider.url}")
String jmsProviderUrl;
//Inbound
@Value("${jms.inbound.qcf.userName}")
private String inboundQcfUserName;
@Value("${jms.inbound.qcf.password}")
private String inboundQcfPassword;
@Value("${jms.inbound.queue.connectionfactory}")
private String inboundQueueConnectionFactory;
@Value("${jms.inbound.queue.name}")
private String inboundQueueName;
@Value("${jms.inbound.initial.cache.size}")
private String inboundInitCacheSize;
//Outbound
@Value("${jms.outbound.topic.connectionfactory}")
private String outboundTopicConnectionFactory;
@Value("${jms.outbound.topic.name}")
private String outboundTopicName;
@Value("${jms.outbound.tcf.userName}")
private String outboundTcfUserName;
@Value("${jms.outbound.tcf.password}")
private String outboundTcfPassword;
@Value("${jms.outbound.initial.cache.size}")
private String outboundInitCacheSize;
@Bean
public InitialContext initialContext() throws NamingException {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
initialContextFactory);
props.put(Context.PROVIDER_URL, jmsProviderUrl);
// Create the initial context from the properties we just created
return new InitialContext(props);
}
@Bean(name="inboundConnectionFactory")
@Qualifier("inboundConnectionFactory")
public ConnectionFactory inboundConnectionFactory() throws NamingException {
//Create a Connection Factory adapter with user credentials
UserCredentialsConnectionFactoryAdapter uccfa = new UserCredentialsConnectionFactoryAdapter();
ConnectionFactory cf = (ConnectionFactory) initialContext().lookup(inboundQueueConnectionFactory);
uccfa.setTargetConnectionFactory(cf);
uccfa.setUsername(inboundQcfUserName);
uccfa.setPassword(inboundQcfPassword);
CachingConnectionFactory ccf = new CachingConnectionFactory();
if(inboundInitCacheSize != null && Integer.parseInt(inboundInitCacheSize) > 0){
ccf.setSessionCacheSize(Integer.parseInt(inboundInitCacheSize)); //Default is 1
}
ccf.setTargetConnectionFactory(uccfa);
return ccf;
}
@Bean(name="inboundJMSTemplate")
@Qualifier("inboundJMSTemplate")
public JmsTemplate inboundJMSTemplate(@Qualifier("inboundConnectionFactory") ConnectionFactory inboundConnectionFactory ) throws NamingException{
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(inboundConnectionFactory);
jmsTemplate.setDefaultDestinationName(inboundQueueName);
return jmsTemplate;
}
@Bean(name="inboundJmsListenerContainerFactory")
@Qualifier("inboundJmsListenerContainerFactory")
public JmsListenerContainerFactory<?> inboundJmsListenerContainerFactory(
DefaultJmsListenerContainerFactoryConfigurer configurer,
@Qualifier("inboundConnectionFactory") ConnectionFactory inboundConnectionFactory)
throws NamingException {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the
// message converter
configurer.configure(factory, inboundConnectionFactory);
// You could still override some of Boot's default if necessary.
return factory;
}
@Bean(name="outboundConnectionFactory")
@Qualifier("outboundConnectionFactory")
public ConnectionFactory outboundConnectionFactory() throws NamingException {
//Create a Connection Factory adapter with user credentials
UserCredentialsConnectionFactoryAdapter uccfa = new UserCredentialsConnectionFactoryAdapter();
ConnectionFactory cf = (ConnectionFactory) initialContext().lookup(outboundTopicConnectionFactory);
uccfa.setTargetConnectionFactory(cf);
uccfa.setUsername(outboundTcfUserName);
uccfa.setPassword(outboundTcfPassword);
CachingConnectionFactory ccf = new CachingConnectionFactory();
if(outboundInitCacheSize != null && Integer.parseInt(outboundInitCacheSize) > 0){
ccf.setSessionCacheSize(Integer.parseInt(outboundInitCacheSize)); //Default is 1
}
ccf.setTargetConnectionFactory(uccfa);
return ccf;
}
@Bean(name="outboundErrorJMSTemplate")
@Qualifier("outboundErrorJMSTemplate")
public JmsTemplate outboundErrorJMSTemplate(@Qualifier("outboundConnectionFactory") ConnectionFactory outboundConnectionFactory ) throws NamingException{
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(outboundConnectionFactory);
jmsTemplate.setDefaultDestinationName(outboundTopicName);
return jmsTemplate;
}
}
我尝试过指定限定符、bean名称。。仍然会出错。有人能建议我应该怎么做来解决这个问题吗。我需要入站和出站连接,因为应用程序将使用和发布通过将@Primary添加到入站连接工厂来解决通过将@Primary添加到入站连接工厂来解决检查此问题,可以帮助您。如果在您的案例中,您混合使用Spring XML和注释来创建连接工厂,那么SpringBoot可能会出现问题,在这种情况下,请在应用程序中禁用Spring boot jms的autostart:
@SpringBootApplication(exclude = {JmsAutoConfiguration.class})
这就是我的情况。检查这个问题,它可以帮助你。如果在您的案例中,您混合使用Spring XML和注释来创建连接工厂,那么SpringBoot可能会出现问题,在这种情况下,请在应用程序中禁用Spring boot jms的autostart:
@SpringBootApplication(exclude = {JmsAutoConfiguration.class})
这就是我的情况。这可能会有所帮助:,谷歌的这句话将返回大量结果:“spring@Configuration multiple beans same class”@ClayFerguson:谢谢你的建议。深入研究发现spring boot正在自动配置JmsListenerContainerFactory bean(由于类路径中的spring jms jars)这就是抛出异常。因此,将入站连接工厂注释为主连接,这似乎解决了问题。您最初的帖子听起来好像您已经尝试了@Primary,否则我会这么说。很高兴你能让它工作。这可能会有帮助:,这个谷歌短语将返回大量结果:“spring@Configuration multiple beans same class”@ClayFerguson:谢谢你的建议。深入研究发现spring boot自动配置了一个JmsListenerContainerFactory bean(由于类路径中的spring jms jars)这就是抛出异常。因此,将入站连接工厂注释为主连接,这似乎解决了问题。您最初的帖子听起来好像您已经尝试了@Primary,否则我会这么说。很高兴你让它工作了。