一.简介
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();