title: Spring root application context and servlet context confusion
body: <p>I know that I need to register classes annotated with <code>@Controller</code> in my servlet context to make my webapp accesible. Usually, I will do it the following way:</p>
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan({&quot;foo.bar.controller&quot;})
  public class WebConfig extends WebMvcConfigurerAdapter {
      //other stuff like ViewResolvers, MessageResolvers, MessageConverters, etc.
  }
  </code></pre>
  <p>All other configuration classes I added to my root application context. Here is how my dispatcher initializer usually look like:</p>
  <pre><code>public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[] { RootConfig.class, ServiceConfig.class };
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[] { WebConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { &quot;/&quot; };
      }
  }
  </code></pre>
  <p>But things are getting more interesting when I started to use WebSockets. To get websockets working, you have to put WebSoketConfig.class to servlet context. Here is my example of WebSocketConfig:</p>
  <pre><code>@Configuration
  @EnableScheduling
  @EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint(&quot;/chat&quot;).withSockJS();
      }
  
      @Override
      public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
          channelRegistration.taskExecutor().corePoolSize(4).maxPoolSize(8);
      }
  
      @Override
      public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
          channelRegistration.taskExecutor().corePoolSize(4).maxPoolSize(8);
      }
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry registry) {
          registry.enableSimpleBroker(&quot;/queue&quot;, &quot;/topic&quot;);
          registry.setApplicationDestinationPrefixes(&quot;/app&quot;);
      }
  
  }
  </code></pre>
  <p>Also, I've created a service to send a message to the topic:</p>
  <pre><code>@Service
  public class TimeServiceWsImpl implements TimeServiceWs {
  
      @Autowired
      private SimpMessagingTemplate messagingTemplate;
  
      @Override
      public void sentCurrentTime() {
          long currentTime = System.currentTimeMillis();
          String destination = &quot;/topic/chatty&quot;;
          logger.info(&quot;sending current time to websocket /topic/time : &quot; + currentTime);
          this.messagingTemplate.convertAndSend(destination, currentTime);
      }
  }
  </code></pre>
  <p>I need to use this service in some other services (Autowire it). And now I'm in a deadlock:</p>
  <ol>
  <li>If I'm trying to create <code>TimeServiceWs</code> bean inside root application context, as expected it doesn't see <code>SimpMessagingTemplate</code> bean and throws <code>NoSuchBeanDefinitionException</code></li>
  <li>If I'm trying to create <code>TimeServiceWs</code> bean inside servlet context, then I'm unable to autowire it to any another service, because root context can't see servlet context beans(as far as I know)</li>
  <li>If I move all my configurations to servlet context, all beans are successfully created, but I get the following exception: <code>java.lang.IllegalStateException: No WebApplicationContext found</code> and can't access my webapp</li>
  </ol>
  <p>What am I supposed to do? What should be inside root context? What should be inside servlet context? And could you please clarify the difference between these context one more time please?</p>
  <p>If you will need any additional information, just let me know.</p>