3

我基于http://bsnyderblog.blogspot.com/2010/02/using-spring-jmstemplate-to-send-jms.html设置了 JMS 应用程序

但是我无法启动tomcat,因为我有以下异常:

    Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsSender': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jms.core.JmsTemplate pl.zleek.test.jms.JmsSender.jmsTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jms.core.JmsTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的 JmsSender 类:

package pl.zleek.test.jms;

@Component
public class JmsSender {

@Autowired
private JmsTemplate jmsTemplate;

public void sendMessage(final String msg) throws JMSException {
    jmsTemplate.send(new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(msg);
        }
    });
}

}

我的bean配置:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:jms="http://www.springframework.org/schema/jms"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="pl.zleek.test" />

<mvc:annotation-driven />

<bean id="amqConnectionFactory"
      class="org.apache.activemq.ActiveMQConnectionFactory"
      p:brokerURL="tcp://localhost:61616" />

<!-- A cached connection to wrap the ActiveMQ connection -->

<bean id="cachedConnectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory"
      p:targetConnectionFactory-ref="amqConnectionFactory"
      p:sessionCacheSize="10" />



<!-- A destination in ActiveMQ -->
<bean id="destination"
      class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="FOO.TEST" />
</bean>



<!-- A JmsTemplate instance that uses the cached connection and destination -->

<bean id="jmsTemplate"
      class="org.springframework.jms.core.JmsTemplate"
      p:connectionFactory-ref="cachedConnectionFactory"
      p:defaultDestination-ref="destination" />

<!-- JMS Consumer Configuration -->
<bean id="jmsConsumerConnectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory"
      p:targetConnectionFactory-ref="amqConnectionFactory"
      p:sessionCacheSize="10" />

<jms:listener-container container-type="default"
                        connection-factory="jmsConsumerConnectionFactory"
                        acknowledge="auto">
    <jms:listener destination="pl.zleek.test.jms" ref="listener" />
</jms:listener-container>

我的另一个spring配置在webapp/WEB-INF/mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:jms="http://www.springframework.org/schema/jms"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="pl.zleek.test" />

<mvc:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

我该如何解决这个问题?IntelliJ Idea 识别 bean jmsTemplate 并且可以轻松地在类和 bean 定义之间导航。

4

0 回答 0