title: How to inject JmsTemplate object using annotations in a spring class?
body: <p>I am trying to inject a JmsTemplate object into a class using <strong>@Autowired</strong>, but it is giving a <strong>NullPointerException</strong> whenever I try to send messages. While in the application class I could instantiate jmsTemplate using the <strong>ApplicationContext</strong> </p>
  
  <pre><code>ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
  </code></pre>
  
  <p>I could not do this in any other class, as I did not figure out how to get the context object.</p>
  
  <p>This is my application class:</p>
  
  <pre><code>@EnableAutoConfiguration
  public class Application {
  
    private static final String maxFileSize = "512KB";
    private static final String maxRequestSize = "512KB";
  
    static String IncomingMailboxDestination = "Responses";
  
    @Bean
    Receiver receiver() {
      return new Receiver();
    }
  
    @Bean
    Producer producer() {
      return new Producer();
    }
  
    @Bean
    MessageListenerAdapter adapter(Receiver receiver) {
      MessageListenerAdapter messageListener
              = new MessageListenerAdapter(receiver);
      messageListener.setDefaultListenerMethod("receiveMessage");
      return messageListener;
    }
  
    @Bean
    SimpleMessageListenerContainer container(MessageListenerAdapter messageListener,
                                           ConnectionFactory connectionFactory) {
      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
      container.setMessageListener(messageListener);
      container.setConnectionFactory(connectionFactory);
      container.setDestinationName(IncomingMailboxDestination);
      return container;
     }
  
    @Bean
    MultipartConfigElement multipartConfigElement() {
      MultipartConfigFactory factory = new MultipartConfigFactory();
      factory.setMaxFileSize(maxFileSize);
      factory.setMaxRequestSize(maxRequestSize);
      return factory.createMultipartConfig();
    }
  
    public static void main(String[] args) {
       // Clean out any ActiveMQ data from a previous run
      FileSystemUtils.deleteRecursively(new File("activemq-data"));
  
      // Launch the application
      ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  
  
  }
  }
  </code></pre>
  
  <p>and this is the Producer class:</p>
  
  <pre><code>@Component("producer")
  public class Producer 
  {
    @Autowired   
    @Qualifier("jmsTemplate")
    private JmsTemplate jmsTemplate;
    private static final Logger logger = Logger.getLogger(Producer.class);
  
  
    public void convertAndSendMessage(String destination, Message notification) 
    {
      logger.info("sending message : " + notification.getMessage());
      jmsTemplate.convertAndSend(destination, notification);
    }
  }
  </code></pre>
  
  <p>This is the full stack trace:</p>
  
  <pre><code>java.lang.NullPointerException: null
  at ncl.jms.Producer.convertAndSendMessage(Producer.java:35)
  at ncl.controllers.ModelController.process(ModelController.java:196)
  at ncl.controllers.ModelController.process(ModelController.java:202)
  at ncl.controllers.ModelController.runModel(ModelController.java:147)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
  at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>What am I missing here?</p>