Spring配置(基于注释):NoUniqueBeanDefinitionException:没有类型' javax.jms.ConnectionFactory'的限定bean。可供选择:

时间:2017-12-02 17:26:57

标签: java spring

spring配置:尝试实例化将使用注释使用特定连接工厂bean实例的JmsMessageListenerContainerFactory Bean。

{12: 34, 89: 67}

预期结果:实例化JmsListenerContainerFactory。

实际:

@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名称..仍然会出错。谁能建议我应该做些什么来解决这个问题。我需要入站和出站连接,因为应用程序将使用和发布

2 个答案:

答案 0 :(得分:1)

通过将@Primary添加到入站连接工厂

来解决

答案 1 :(得分:0)

检查这个问题 Spring Boot multiple JMS connections 可以帮助您。如果在您的情况下使用 Spring XML 和注释的混合来创建连接工厂,那么您可能会遇到 SpringBoot 问题,在这种情况下,您可以在应用程序中禁用 spring-boot-jms 的自动启动:

$('#table_id')

这是我的情况。