In a Spring WebSocket application, @Configuration marks the class where you declare the WebSocket infrastructure as beans: which handlers exist, which URLs they serve, which interceptors and origin rules apply. Combined with @EnableWebSocket (raw WebSocket) or @EnableWebSocketMessageBroker (STOMP messaging), it is the single place Spring reads to wire the endpoints at startup.
@Configuration itself is not WebSocket specific. It is the general Spring annotation for Java-based configuration, and the WebSocket support builds on it with two enabling annotations and two callback interfaces.
A class annotated with @Configuration is a source of bean definitions. Spring processes it at startup, runs its @Bean methods, and registers the results in the application context. For WebSocket this gives you three things:
WebSocketHandler).Spring Boot also needs the spring-boot-starter-websocket dependency, which pulls in spring-websocket, spring-messaging and the servlet container support.
For plain WebSocket you add @EnableWebSocket and implement WebSocketConfigurer:
Spring calls registerWebSocketHandlers once while building the context. The chatHandler() method is a @Bean, so there is one shared handler instance, and other beans can be injected into a handler class instead of the anonymous class shown here.
Connect from a browser to test it:
For publish and subscribe style messaging, use @EnableWebSocketMessageBroker and implement WebSocketMessageBrokerConfigurer:
Controllers then use @MessageMapping("/chat") to receive messages sent to /app/chat and @SendTo("/topic/messages") to broadcast results. The simple broker runs in memory; for several server instances, configure an external broker through enableStompBrokerRelay.
| Need | Use |
|---|---|
| Custom binary or text protocol, full control of frames | @EnableWebSocket with WebSocketConfigurer |
| Topics, user destinations, message routing, browser clients with a STOMP library | @EnableWebSocketMessageBroker with WebSocketMessageBrokerConfigurer |
| Reactive stack | WebSocketHandler from spring-webflux, mapped with HandlerMapping beans |
setAllowedOrigins("*") in production. It lets any web page open a socket to your server using the visitor's cookies. List your own origins, or use setAllowedOriginPatterns.@EnableWebSocketSecurity and message matchers for STOMP.wss://.ServletServerContainerFactoryBean (maximum text message size, idle timeout) to bound memory per session.Spring needs the configuration class registered as a bean. @Configuration is the standard way, and @EnableWebSocket or @EnableWebSocketMessageBroker must appear on it.
The first exposes raw handlers. The second adds STOMP, a message broker and annotated controllers on top of WebSocket.
Usually an origin mismatch or a Spring Security rule blocking the endpoint path. Check setAllowedOriginPatterns and your security filter chain.
Yes, call withSockJS() on the registration for browsers or proxies that cannot use native WebSocket. Most current browsers support WebSocket, so it is a fallback rather than a default.
@Configuration is where Spring WebSocket is assembled: handlers as beans, endpoints and interceptors in a configurer callback, and the choice between raw WebSocket and STOMP through the enabling annotation. Restrict allowed origins, secure the handshake, and keep handler logic in injectable beans.