title: Spring MVC can't access @Autowired fields from @MessageMapping annotated methods
body: <p>I've been setting up my Spring 4 MVC application to work with STOMP over WebSocket and so far i've succeeded, my servlet can handle and dispatch STOMP messages without problems. </p>
  
  <p>However, i've encountered an annoying problem when handling these messages from @MessageMapping annotated methods inside my controllers: I can't access any of the @Autowired controller's fields from inside these methods (they all are null pointers), but i can access these fields on the same controller from @RequestMapping annotated methods without any problem.</p>
  
  <p>My Dispatcher Servlet Config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:oxm="http://www.springframework.org/schema/oxm"
      xmlns:security="http://www.springframework.org/schema/security"
      xmlns:websocket="http://www.springframework.org/schema/websocket"
      xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
          http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"&gt;
  
  
      &lt;mvc:annotation-driven/&gt;
      &lt;mvc:resources location="assets" mapping="/assets/**"/&gt;
      &lt;mvc:resources location="assets/img/favicon.ico" mapping="/favicon.ico" /&gt;
  
      &lt;context:component-scan base-package="com.company.web.controller"/&gt;
  
      &lt;security:global-method-security pre-post-annotations="enabled" /&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
          &lt;property name="order" value="1" /&gt;
          &lt;property name="contentNegotiationManager"&gt;
              &lt;bean class="org.springframework.web.accept.ContentNegotiationManager"&gt;
                  &lt;constructor-arg&gt;
                      &lt;bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"&gt;
                          &lt;constructor-arg&gt;
                              &lt;map&gt;
                                  &lt;entry key="json" value="application/json" /&gt;
                                  &lt;entry key="xml" value="application/xml" /&gt;
                              &lt;/map&gt;
                          &lt;/constructor-arg&gt;
                      &lt;/bean&gt;
                  &lt;/constructor-arg&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  
      &lt;mvc:interceptors&gt;
          &lt;bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/&gt;
          &lt;bean class="com.hxplus.web.interceptor.aCustomAwesomeInterceptor"/&gt;
      &lt;/mvc:interceptors&gt;
  
      &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="es"/&gt;
  
      &lt;bean id="messageSource" 
      class="org.springframework.context.support.ResourceBundleMessageSource" 
      p:basename="messages"&gt;&lt;/bean&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
       p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/&gt;
  
       &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/&gt;
  
       &lt;bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
  
           &lt;!-- setting maximum upload size --&gt;
          &lt;property name="maxUploadSize" value="${upload.limit}" /&gt;
  
      &lt;/bean&gt;
  
      &lt;context:property-placeholder location="classpath*:upload_config.properties"/&gt;
  
      &lt;websocket:message-broker application-destination-prefix="/app"&gt;
          &lt;websocket:stomp-endpoint path="/hello/{recipient}"&gt;
              &lt;websocket:sockjs/&gt;
          &lt;/websocket:stomp-endpoint&gt;
          &lt;websocket:simple-broker prefix="/topic" /&gt;
      &lt;/websocket:message-broker&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My Controller:</p>
  
  <pre><code>@Controller
  public class TheController {
      private static final Logger _logger = LoggerFactory.getLogger(TheController.class);
  
      @Autowired private TheService theService;
      @Autowired private SimpMessagingTemplate simpMessagingTemplate;
  
      @PreAuthorize("hasRole('GOD')")
      @RequestMapping(value = "/something/{id}", method = RequestMethod.GET)
      public String show(Model model, @PathVariable("id") Long id) {
          //HERE I CAN ACCESS BOTH "theService" AND
          //"simpMessagingTemplate" WITHOUT PROBLEMS
      }
  
      @MessageMapping("/hello/{recipient}")
      private VOID testing(StompEvent event, @DestinationVariable String recipient){
          //HERE BOTH "theService" AND "simpMessagingTemplate" ARE NULL
      }
  }
  </code></pre>