本文主要是介绍Spring5高级编程------Spring-WebSocket中STOMP配置的加载机制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring-WebSocket中STOMP配置的加载机制
- WebSocketMessageBrokerConfigurer配置类的加载
- 一、注解@EnableWebSocketMessageBroker
- 二、DelegatingWebSocketMessageBrokerConfiguration配置类
- 三、WebSocketMessageBrokerConfigurationSupport抽象类
- 四、AbstractMessageBrokerConfiguration抽象类
- 五、STOMP配置中的三个关键接口
- 1、StompEndpointRegistry
- 2、StompWebSocketEndpointRegistration
- 3、SockJsServiceRegistration
WebSocketMessageBrokerConfigurer配置类的加载
一、注解@EnableWebSocketMessageBroker
将这个注释添加到一个@Configuration类中,这样就可以使用更高级的消息传递子协议在WebSocket上启用代理支持的消息传递。@EnableWebSocketMessageBroker注解的主要作用就是引入一个配置类DelegatingWebSocketMessageBrokerConfiguration
package org.springframework.web.socket.config.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;/*** Add this annotation to an {@code @Configuration} class to enable broker-backed* messaging over WebSocket using a higher-level messaging sub-protocol.**/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebSocketMessageBrokerConfiguration.class)
public @interface EnableWebSocketMessageBroker {}
将这个注解放在自定义的WebSocketMessageBrokerConfigurer实现类上即可实现STOMP配置的自动加载,自定义STOMP配置类如下(@Configuration注解不是必须的,也可以使用@Import注解直接在主配置类中引入该配置类):
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.context.annotation.Configuration;@EnableWebSocketMessageBroker
@Configuration
public class WebSocketStompConfiguration implements WebSocketMessageBrokerConfigurer {
}
注意:WebSocketMessageBrokerConfigurer有一个空实现的抽象类AbstractWebSocketMessageBrokerConfigurer,由于WebSocketMessageBrokerConfigurer接口提供了默认实现,所以AbstractWebSocketMessageBrokerConfigurer已被废弃,这里不建议使用继承AbstractWebSocketMessageBrokerConfigurer的方式配置。
二、DelegatingWebSocketMessageBrokerConfiguration配置类
@EnableWebSocketMessageBroker 注解使用@Import()注解引入了该配置类源码如下:
package org.springframework.web.socket.config.annotation;import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.util.CollectionUtils;
/*** A {@link WebSocketMessageBrokerConfigurationSupport} extension that detects* beans of type {@link WebSocketMessageBrokerConfigurer} and delegates to all* of them allowing callback style customization of the configuration provided* in {@link WebSocketMessageBrokerConfigurationSupport}.** <p>This class is typically imported via {@link EnableWebSocketMessageBroker}.** @author Rossen Stoyanchev* @since 4.0*/
@Configuration
public class DelegatingWebSocketMessageBrokerConfiguration extends WebSocketMessageBrokerConfigurationSupport {private final List<WebSocketMessageBrokerConfigurer> configurers = new ArrayList<>();@Autowired(required = false)public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) {if (!CollectionUtils.isEmpty(configurers)) {this.configurers.addAll(configurers);}}@Overrideprotected void registerStompEndpoints(StompEndpointRegistry registry) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.registerStompEndpoints(registry);}}@Overrideprotected void configureWebSocketTransport(WebSocketTransportRegistration registration) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.configureWebSocketTransport(registration);}}@Overrideprotected void configureClientInboundChannel(ChannelRegistration registration) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.configureClientInboundChannel(registration);}}@Overrideprotected void configureClientOutboundChannel(ChannelRegistration registration) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.configureClientOutboundChannel(registration);}}@Overrideprotected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.addArgumentResolvers(argumentResolvers);}}@Overrideprotected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.addReturnValueHandlers(returnValueHandlers);}}@Overrideprotected boolean configureMessageConverters(List<MessageConverter> messageConverters) {boolean registerDefaults = true;for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {if (!configurer.configureMessageConverters(messageConverters)) {registerDefaults = false;}}return registerDefaults;}@Overrideprotected void configureMessageBroker(MessageBrokerRegistry registry) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.configureMessageBroker(registry);}}}
这个配置类的第一个作用是发现并收集Spring容器中的所有WebSocketMessageBrokerConfigurer类型的配置类,代码如下:
private final List<WebSocketMessageBrokerConfigurer> configurers = new ArrayList<>();@Autowired(required = false)public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) {if (!CollectionUtils.isEmpty(configurers)) {this.configurers.addAll(configurers);}}
这个配置类的第二个作用是实现父类的8个方法,在这8个方法中会遍历获取所有WebSocketMessageBrokerConfigurer 类并分别调用对应的配置方法,从而引入用户自定义的配置,代码如下(这里只列举一个,其他7个类似):
@Overrideprotected void registerStompEndpoints(StompEndpointRegistry registry) {for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {configurer.registerStompEndpoints(registry);}}
三、WebSocketMessageBrokerConfigurationSupport抽象类
WebSocketMessageBrokerConfigurationSupport是DelegatingWebSocketMessageBrokerConfiguration的抽象父类。其中包含2个需要DelegatingWebSocketMessageBrokerConfiguration实现的方法:
public abstract class WebSocketMessageBrokerConfigurationSupport extends AbstractMessageBrokerConfiguration {protected void configureWebSocketTransport(WebSocketTransportRegistration registry) {}protected abstract void registerStompEndpoints(StompEndpointRegistry registry);}
四、AbstractMessageBrokerConfiguration抽象类
AbstractMessageBrokerConfiguration是DelegatingWebSocketMessageBrokerConfiguration的抽象父类。其中包含6个需要DelegatingWebSocketMessageBrokerConfiguration实现的方法:
public abstract class AbstractMessageBrokerConfiguration implements ApplicationContextAware {protected void configureClientInboundChannel(ChannelRegistration registration) {}protected void configureClientOutboundChannel(ChannelRegistration registration) {}protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {}protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {}protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {return true;}protected void configureMessageBroker(MessageBrokerRegistry registry) {}
}
五、STOMP配置中的三个关键接口
1、StompEndpointRegistry
2、StompWebSocketEndpointRegistration
3、SockJsServiceRegistration
这篇关于Spring5高级编程------Spring-WebSocket中STOMP配置的加载机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!