title: Autowiring SimpMessagingTemplate
body: <p>I've run into trouble trying to use SimpMessagingTemplate in a Service class. Here are the relevant Code Snippets:</p>
  
  <p><strong>UserService.java</strong> - Autowiring fails, template = null</p>
  
  <pre><code>@Service
  public class UserService{
      @Autowired
      private SimpMessagingTemplate template;
  
      // Some Code
  
      public void tellUser(String username, String url) {
          // This is always true
          System.out.println("TEMPLATE NULL? " +(this.template == null));
          // Further code omitted
      }
  }
  </code></pre>
  
  <p><strong>SocketController.java</strong> - Autowiring works, Spring Websockets work as inteded.</p>
  
  <pre><code>@Controller
  public class WebSocketController {
      @Autowired
      private SimpMessagingTemplate template;
  
      public void tellUser(String username, String url) {
          // False here, template is autowired correctly
          System.out.println("TEMPLATE NULL? " +(this.template == null));
          this.template.convertAndSendToUser(username, "/test/notify", new Greeting("USER SPECIFIC MESSAGE: " + url));
      }
  }
  </code></pre>
  
  <p><strong>WebSocketConfig.java</strong> </p>
  
  <pre><code>@Configuration
  @EnableScheduling
  @EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry config) {
          config.enableSimpleBroker("/user", "/test");
          config.setApplicationDestinationPrefixes("/app");
      }
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint("/test").withSockJS();
  
      }
  }
  </code></pre>
  
  <p>So basically the autowiring works in my Controller class, but not in my Service. I do need to invoke a convertAndSendToUser manually in a Service. What's the difference for Autowiring SimpMessagingTemplate between a Controller and a Service? Any input on why this might happen is greatly appreciated.</p>