我一定很容易错过一些事情。这是我的Spring配置XML的片段:

<bean id="queueDestination" class="com.ibm.mq.jms.MQQueue">
    <constructor-arg value="TestQ" />
</bean>

<bean id="topicDestination" class="com.ibm.mq.jms.MQTopic">
    <constructor-arg value="TestTopic" />
</bean>

<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
        <property name="defaultDestination" ref="queueDestination" />
</bean>

<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsTopicConnectionFactory" />
        <property name="defaultDestination" ref="topicDestination" />
</bean>

这是尝试将其自动接线的代码:
@Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsQueueTemplate;

@Autowired
@Qualifier("queueDestination")
private Destination queueDestination;

我不断得到:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'publish': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jms.core.JmsTemplate com.vulab.publishsubscribe.Publish.jmsTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.jms.core.JmsTemplate] is defined: expected single matching bean but found 2: [jmsQueueTemplate, jmsTopicTemplate]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

请帮助我了解我所缺少的。

我觉得这与@Autowire或@Qualifier无关。我认为这是因为我有两个用“org.springframework.jms.core.JmsTemplate”类定义的bean。

谢谢。

最佳答案

我将您的代码与this tutorial进行了比较。我认为您的bean中不需要<qualifier/>属性。相反,您可以使用Bean ID作为@Qualifier批注中的值。

另外,请确保在applicationContext.xml的顶部注册AutowiredAnnotationBeanPostProcessor。

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Here's another tutorial,如果您向下滚动,会发现一个带有@Qualifier的示例。

09-27 23:05