java - Tomcat8 WebSockets (JSR-356) with Guice 3.0 -


i trying @inject guice service @serverendpoint. using tomcat 8.0.15 jsr-356 implementation. however, dependency injection isn't working. there additional configuration needs done in order enable guice injection? note using standard javax annotations only.

i figured out. websocket endpoint needs have custom configurator, creates , returns instances using guice injector instance.

example:

custom guice servlet context listener:

public class customservletcontextlistener extends guiceservletcontextlistener {      public static injector injector;      @override     protected injector getinjector() {         injector = guice.createinjector(...);         return injector;     } } 

websockets custom configurator:

public class customconfigurator extends configurator {   @override   public <t> t getendpointinstance(class<t> clazz)         throws instantiationexception {     return customservletcontextlistener.injector.getinstance(clazz);   } } 

and in websocket endpoint:

@serverendpoint(value = "/ws/sample_endpoint", configurator = customconfigurator.class) public class sampleendpoint {   private final someservice service;    @inject   public sampleendpoint(someservice service) {     this.service = service;   }   ... } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -