问题
From WebSocket Endpoint I try to call Singleton Service. But I an unable to use Request or Session scope from WebSocket.
(@Scope( value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS))
I get Scope 'request' is not active for the current thread; For 'request' or 'session' scope on any 'ScopedProxyMode'.
Thanks for the help!
回答1:
For web sockets there are no request/response so the request scope is invalid. They introduced new scope called websocket in Spring 4.1. @Scope(name = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS). Example reference link
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#websocket-stomp-websocket-scope
回答2:
I'm not sure about Spring, but if you built your web socket code to JSR-356 standard (which spring should understand) you can add a custom configurator to the endpoint definition where you can intercept the handshake. See javax.websocket.server.ServerEndpointConfig.Configurator.modifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse)
then you may be able to get your hands on the actual session object but that would depend on your application server. You can definitely get any parameters and headers.
This is the immediate answer, however the better design approach would be to remove any reliance on sessions in the first place.
来源:https://stackoverflow.com/questions/31916727/request-or-session-scope-in-spring-websocket