前提
实现对客户端的在线统计,及与客户端的交互和接受redis的消息
设置spring上下文
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* <b><code>ApplicationContextRegister</code></b>
* <p>
* class_comment
* </p>
* <b>Create Time:</b> 2019/12/30 16:40
*
* @author ong
* @version 0.0.1
* @since core-be 0.0.1
*/
@Component
@Lazy(false)//不延时代表查询出对象A的时候,会把B对象也查询出来放到A对象的引用中,A对象中的B对象是有值的。
public class ApplicationContextRegister implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;
/**
* 设置spring上下文 * * @param applicationContext spring上下文 * @throws BeansException * author:huochengyan https://blog.csdn.net/u010919083
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
APPLICATION_CONTEXT = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
}
WebSocketConfig 类
首先要注入ServerEndpointExporter,这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint。要注意,如果使用独立的servlet容器,而不是直接使用springboot的内置容器,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* <b><code>WebSocketConfig</code></b>
* <p>
* class_comment
* </p>
* <b>Create Time:</b> 2019/12/9 15:58
*
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
如上图所示,就知道,为什么在组件类