tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Williams <nicho...@nicholaswilliams.net>
Subject Re: @ServerEndpoint Guice
Date Wed, 06 Nov 2013 15:23:00 GMT

On Nov 6, 2013, at 7:55 AM, Marko Sanković wrote:

> Hi,
> 
> For the last couple of hours I've been trying to inject a simple object
> into the class that is @ServerEndpoint annotated.
> 
> As stated: Tomcat implements the Java WebSocket 1.0 API defined by JSR-356.
> 
> I'm using Guice as dependency injection framework and Tomcat 7.0.47.
> 
> This is how my websocket server endpoint looks like:
> 
> ...
> import com.google.inject.Inject;
> ...
> @ServerEndpoint("/wsendpoint")
> public class WsEndpoint {
> 
>    @Inject
>    InjectedSimpleBean injectedSimpleBean;
> 
>    ...
> }
> 
> I can connect to this endpoint, send and receive messages, but
> injectedSimpleBean attribute is null (as expected).
> 
> I guess I will have to change the
> way java/org/apache/tomcat/websocket/server/DefaultServerEndpointConfigurator.java
> instantiates endpoint class, the getEndpointInstace method will have to
> call something like:
> 
> injector.getInstance(clazz);
> 
> but, then again the DefaultServerEndpointConfiguration will also have be
> instantiated by the injector.
> 
> Any help would be appreciated. Thanks

Changing the Tomcat-specific class won't be necessary. You can do this with just the API.
In your @ServerEndpoint annotation, you need to specify a different configuration that extends
javax.websocket.server.ServerEndpointConfig.Configurator. Spring Framework has a class [1]
that does just this for its DI and other bean processing. You can probably create a class
modeled after that.

Nick

[1] https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/SpringConfigurator.java?source=cc
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message