Spring JMS - autowire JmsTemplate bean failed

zleek picture zleek · Nov 20, 2014 · Viewed 7.1k times · Source

I set up JMS application based on the http://bsnyderblog.blogspot.com/2010/02/using-spring-jmstemplate-to-send-jms.html

However I cannot start tomcat because I've got following exeption:

    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)}

My JmsSender class:

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);
        }
    });
}

}

My bean configuration:

<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>

My other spring configuration is in 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>

How can I solve this issue? IntelliJ Idea recognize bean jmsTemplate and could easily navigate between class and bean definition.

Answer