Provide @WebSocketScope annotation and public SCOPE_WEBSOCKET constant

Closes gh-35235
This commit is contained in:
Juergen Hoeller
2025-07-26 11:38:02 +02:00
parent 7316aab048
commit c7fbf7809f
3 changed files with 74 additions and 6 deletions
@@ -1,8 +1,8 @@
[[websocket-stomp-websocket-scope]]
= WebSocket Scope
Each WebSocket session has a map of attributes. The map is attached as a header to
inbound client messages and may be accessed from a controller method, as the following example shows:
Each WebSocket session has a map of attributes. The map is attached as a header to inbound
client messages and may be accessed from a controller method, as the following example shows:
[source,java,indent=0,subs="verbatim,quotes"]
----
@@ -20,13 +20,13 @@ public class MyController {
You can declare a Spring-managed bean in the `websocket` scope.
You can inject WebSocket-scoped beans into controllers and any channel interceptors
registered on the `clientInboundChannel`. Those are typically singletons and live
longer than any individual WebSocket session. Therefore, you need to use a
scope proxy mode for WebSocket-scoped beans, as the following example shows:
longer than any individual WebSocket session. Therefore, you need to use
WebSocket-scoped beans in proxy mode, conveniently defined with `@WebSocketScope`:
[source,java,indent=0,subs="verbatim,quotes"]
----
@Component
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
@WebSocketScope
public class MyBean {
@PostConstruct