title: Springboot @ServerEndPoint "Failed to find the root WebApplicationContext."
body: <p><strong>I'm having trouble using spring with @ServerEndPoint annotated class</strong></p>
  
  <p>i'm using Springboot 1.2.3 and i'm trying to figure it out how to have a single instance of the endpoint </p>
  
  <pre><code>@SpringBootApplication
  @EnableJpaRepositories
  @EnableWebSocket
  public class ApplicationServer {
      public static void main(String[] args) {
          SpringApplication.run(ApplicationServer.class, args);
      }
  }
  </code></pre>
  
  <p>Spring configuration:</p>
  
  <pre><code>@ConditionalOnWebApplication
  @Configuration
  public class WebSocketConfigurator {
  
      @Bean
      public ServerEndPoint serverEndpoint() {
          return new ServerEndPoint();
      }
  
      @Bean
      public ServerEndpointExporter serverEndpointExporter() {
          return new ServerEndpointExporter();
      }
  }
  </code></pre>
  
  <p>WebSocket endpoint:</p>
  
  <pre><code>@ServerEndpoint(value = "/", decoders = MessageDecoder.class, 
  encoders = MessageEncoder.class, configurator = SpringConfigurator.class)
  public class ServerEndPoint {
  
      private static final Logger LOG = LoggerFactory.getLogger(ServerEndPoint.class);
  
      public static final Set&lt;CommunicationObserver&gt; OBSERVERS = Sets.newConcurrentHashSet();
  
      @OnMessage
      public void onMessage(Session session, Message msg) {
          LOG.debug("Received msg {} from {}", msg, session.getId());
          for (CommunicationObserver o : OBSERVERS) {
              o.packetReceived(session, msg);
          }
      }
  </code></pre>
  
  <p>This is based on the <a href="https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support" rel="nofollow noreferrer">Spring WebSocket JSR-356 tutorial</a>, but i've got the following error: </p>
  
  <pre><code>java.lang.IllegalStateException: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
      at org.springframework.web.socket.server.standard.SpringConfigurator.getEndpointInstance(SpringConfigurator.java:68)
      at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:50)
      at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:138)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
      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:745)
  </code></pre>
  
  <p>I have both tested in embedded mode and with external tomcat 8 and jetty 9 (in external mode, i remove de Spring config file) but the same error appeares.</p>
  
  <p><a href="https://stackoverflow.com/questions/29306854/serverendpoint-and-autowired">the only workaround i've found is to create a custom configurator.</a></p>
  
  <pre><code>public class SpringEndpointConfigurator extends ServerEndpointConfig.Configurator {
  
      private static WebApplicationContext wac;
  
      public SpringEndpointConfigurator() {
      }
  
      public SpringEndpointConfigurator(WebApplicationContext wac) {
          SpringEndpointConfigurator.wac = wac;
      }
  
      @Override
      public &lt;T&gt; T getEndpointInstance(Class&lt;T&gt; endpointClass) throws InstantiationException {
          T endPoint = wac.getAutowireCapableBeanFactory().getBean(endpointClass);
          return (endPoint != null) ? endPoint : wac.getAutowireCapableBeanFactory().createBean(endpointClass);
      }
  </code></pre>
  
  <p>it is created as a @Bean with the parameterized constructor.</p>
  
  <p>I must have missed something to get it done with the SpringConfigurator class, but i don't know what.</p>