springboot WebSocket通信

一.简介

Servlet3.0的出现带来许多新特性大大的简化了web应用的开发,主要新特性有以下几个:

1.引入注解配置(例如:@WebServlet,@WebFilter,@WebListener,@ServerEndpoint)
2.简化文件上传(例如:@MultipartConfig,Part)
3.Websocket实时通信

二.Servlet 3.0注解实现

1.新建springboot 项目,pom.xml 增加 如下依赖:

 <dependency>
   	 <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-websocket</artifactId>
 </dependency>

2.创建ServerEndpointExporter并注入Spring容器:

@Bean
 ServerEndpointExporter serverEndpointExporter(){
     ServerEndpointExporter exporter = new ServerEndpointExporter();
     return exporter;
 }

该对象主要用于注册使用@ServerEndpoint 标注的类,文档说明如下:

在这里插入图片描述

3.编写工具类为@ServerEndpoint标注的类注入@Autowired 的对象

package com.vincent.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;


@Component
public class SpringUtils implements ApplicationContextAware {
   
    public static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   
        context = applicationContext;
    }

    /**
     * 为bean对象注入使用@Autowired、@Resource 标注的对象
     * @param bean
     */
    public static void processInjectedBean(Object bean){
   
        Class cls = bean.getClass();
        while(cls != null){
   
            Field[] fields = cls.getDeclaredFields();
            
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值