spring boot中使用servlet、listener和filter
spring boot中支持使用java Web三大组件(servlet、listener和filter),但是坑比较多,主要是spring boot内嵌tomcat和独立tomcat服务器有一些细节上的不同,踩完之后,特有此记。
首先说明下,需要实现的功能,网站中有一些需要进行中英文对照的东西,一开始是写在properties配置文件中,经常修改的话,非常麻烦,还需要重启服务器。
初步考虑觉得采用ServletConetext监听器的触发,从mysql表中加载所以中英文对照数据,存入map,保存在ServletContext中,这种方式简单有效,缺点在于如果一旦改动,就需要重启tomcat。
进一步的想法是采用HttpSessionListener监听器,每个用户开始会话的session创建阶段,获取一次map,保存到ServletContext,所有的request都从全局变量ServletContext取,只存一份,实时更新,即使有变化也无需重启服务器。
一、spring boot内嵌服务器
1.session监听器的实现
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
@Autowired
private AppMetaService appMetaService; @Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
Map<String, String> keyMap = appMetaService.getKeyMap();
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
servletContext.setAttribute("keyMap",keyMap);
} @Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
}
}
2.session监听无效
这里有一个问题,发现controller中的请求并不会主动生成session,默认情况下session监听器什么也监听不到。所以需要在每次请求时,手动调用request.getSession触发session的创建。
由于每次请求都需要这些动作,因此我把它加入到了一个过滤器中。
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 在servlet过滤器中触发getSession方法,一次编写,多处使用
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.getSession(true);
filterChain.doFilter(request, servletResponse);
} @Override
public void destroy() { }
}
3.内嵌服务器支持
Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。唯一需要的就是在项目的入口类中加一个注解@ServletComponentScan,顾名思义,它会扫描所有的
@WebServlet、@WebFilter、@WebListener完成对象的注入。
@SpringBootApplication
@ServletComponentScan
public class DataboardApplication { public static void main(String[] args) {
SpringApplication.run(DataboardApplication.class, args);
}
}
到此为止,简洁明了,不废话,本地测试成功,直接上tomcat服务器。
二、独立tomcat服务器
最大的坑来了,在本地跑的很正常的程序,在服务器上跑不动,最奇怪的是有些接口能用,有些不能用,经过多方观察,发现用到servlet、listener和filter的都挂了。
搜了很多资料之后,还是在spring boot 2.01的文档中找到这么一句话:
Tip
@ServletComponentScan has no effect in a standalone container, where the container’s builtin
discovery mechanisms are used instead.
翻译一下就是@ServletComponentScan这个注解在独立tomcat服务器不可用。
只好退求其次,采用@Bean注入的方式
1.对应的监听器代码
@Component
public class MyHttpSessionListener implements HttpSessionListener {
@Autowired
private AppMetaService appMetaService; @Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
Map<String, String> keyMap = appMetaService.getKeyMap();
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
servletContext.setAttribute("keyMap",keyMap);
} @Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
}
}
2.对应过滤器代码
@Component
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 在servlet过滤器中触发getSession方法,一次编写,多处使用
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.getSession(true);
filterChain.doFilter(request, servletResponse);
} @Override
public void destroy() { }
}
3.启动类代码
@SpringBootApplication
public class DataboardApplication {
@Bean
public ServletListenerRegistrationBean sessionHandler() {
return new ServletListenerRegistrationBean<>(new MyHttpSessionListener());
} @Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean myFilter = new FilterRegistrationBean(new MyFilter());
myFilter.addUrlPatterns("/*");
return myFilter;
} public static void main(String[] args) {
SpringApplication.run(DataboardApplication.class, args);
}
}
这里变化最大,去掉了@ServletComponentScan,在类内部增加了两个生成bean的方法。
到此,大功告成!
spring boot中使用servlet、listener和filter的更多相关文章
- 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)
在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...
- Spring boot中使用servlet filter
Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...
- Spring Boot中使用Servlet与Filter
在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...
- Spring boot中注册Servlet
Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...
- spring boot 中使用servlet
- Spring Boot中一个Servlet主动断开连接的方法
主动断开连接,从而返回结果给客户端,并且能够继续执行剩余代码. 对于一个HttpServletResponse类型的对象response来说,执行如下代码: response.getWriter(). ...
- 传统的Servlet在spring boot中怎么实现的?
传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...
- Spring Boot 自定义注册 Servlet、Filter、Listener
前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...
- (7)Spring Boot web开发 --- servlet容器
文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...
随机推荐
- vmware tools 的安装(Read-only file system 的解决)
安装 vmware tools 之后,才能将 vmware 创建的虚拟机以全屏的形式显示. 下载:在 vmware 软件的菜单栏点击[虚拟机],在[虚拟机]的主菜单中选择[安装 VMware Tool ...
- iOS的安全性和越狱
提到越狱,很多人第一反应大概是免费的游戏和 app. 作为软件从业人员,深知中国的大环境的特殊性.内有用户想吃免费午餐的不良付费习惯,外有行业内大头诸如某讯对创新的绞杀.大家对越狱是为了免费.盗版软件 ...
- C++中的模板编程
一,函数模板 1.函数模板的概念 C++中提供了函数模板,所谓函数模板,实际上是建立一个通用函数,其函数的返回值类型和函数的参数类型不具体指定,用一个虚拟的类型来表示.这个通用函数就被称为函数的模板. ...
- 【翻译自mos文章】对于每个文件的 file.id and file.incarnation number,重命名文件别名
对于每个文件的 file.id and file.incarnation number,重命名文件别名 參考原文: Rename Alias of Datafile as Per file.id an ...
- Netty In Action中文版 - 第六章:ChannelHandler
本章介绍 ChannelPipeline ChannelHandlerContext ChannelHandler Inbound vs outbound(入站和出站) 接受连接或创建他们仅仅是你的应 ...
- C# WPF 实现鼠标固定在指定范围内运动
原文:C# WPF 实现鼠标固定在指定范围内运动 一.背景: 需要实现带有三个屏幕,三个屏幕分别显示窗体,但鼠标只能在主窗体中运动,不能移动到其他的两个附屏中. 二.实现: 具体实现使用的是u ...
- ASP.NET Core Identity 验证特性 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Identity 验证特性 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 验证特性 上一章节我们简单介绍了 ...
- python 教程 第二十一章、 扩展Python
第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...
- 微信小程序之商品属性分类
所提及的购物数量的加减,现在说说商品属性值联动选择. 为了让同学们有个直观的了解,到电商网截了一个图片,就是红圈所示的部分 现在就为大家介绍这个小组件,在小程序中,该如何去写 下图为本项目的图: wx ...
- SharePoint 2010 WebPart Web部分 总的膏
SharePoint 2010 WebPart Web部分 总的膏 之前写SharePoint WebPart Web部分相关的博客,我们没有做一个索引.网友在查看的时候非常不方便,于 ...