多连接工厂

Multiple Connection Factories

我正在使用 spring 启动并设置多个连接工厂(pooled 和 jms)。我遇到的问题是它似乎试图将它们自动连接到 javax.jms.ConnectionFactory 中的同一个对象。所以它导致了这个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.jms.ConnectionFactory org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration.connectionFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.jms.ConnectionFactory] is defined: expected single matching bean but found 2: jmsConnectionFactory,pooledConnectionFactory
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 38 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.jms.ConnectionFactory] is defined: expected single matching bean but found 2: jmsConnectionFactory,pooledConnectionFactory
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 40 more

这是我正在使用的配置:

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
   <property name="brokerURL" value="***" />
</bean>
 <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
   <property name="maxConnections" value="10" />
   <property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig" 
   class="org.apache.camel.component.jms.JmsConfiguration">
   <property name="connectionFactory" ref="pooledConnectionFactory"/>
   <property name="acknowledgementModeName" value="CLIENT_ACKNOWLEDGE"/>     
   <property name="concurrentConsumers" value="11"/> 
   <property name="maxConcurrentConsumers" value="11"/> 
</bean>  
<bean id="activemq" 
    class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig"/>
</bean>

我假设问题是这两个 class 都实现了 javax.jms.ConnectionFactory class,然后它们通过 org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration 中的 ConnectionFactory 自动装配。我在这里遗漏了什么吗,我们将不胜感激?

您可以在其中一个连接工厂 bean 上设置 primary=true