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}.
 *
 * 

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); } } @Override protected void registerStompEndpoints(StompEndpointRegistry registry) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.registerStompEndpoints(registry); } } @Override protected void configureWebSocketTransport(WebSocketTransportRegistration registration) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.configureWebSocketTransport(registration); } } @Override protected void configureClientInboundChannel(ChannelRegistration registration) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.configureClientInboundChannel(registration); } } @Override protected void configureClientOutboundChannel(ChannelRegistration registration) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.configureClientOutboundChannel(registration); } } @Override protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.addArgumentResolvers(argumentResolvers); } } @Override protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { configurer.addReturnValueHandlers(returnValueHandlers); } } @Override protected boolean configureMessageConverters(List<MessageConverter> messageConverters) { boolean registerDefaults = true; for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { if (!configurer.configureMessageConverters(messageConverters)) { registerDefaults = false; } } return registerDefaults; } @Override protected 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个类似):

@Override
	protected 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配置的加载机制)