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监听器的实现

  1. @WebListener
  2. public class MyHttpSessionListener implements HttpSessionListener {
  3. @Autowired
  4. private AppMetaService appMetaService;
  5.  
  6. @Override
  7. public void sessionCreated(HttpSessionEvent httpSessionEvent) {
  8. Map<String, String> keyMap = appMetaService.getKeyMap();
  9. ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
  10. servletContext.setAttribute("keyMap",keyMap);
  11. }
  12.  
  13. @Override
  14. public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
  15. }
  16. }

2.session监听无效

这里有一个问题,发现controller中的请求并不会主动生成session,默认情况下session监听器什么也监听不到。所以需要在每次请求时,手动调用request.getSession触发session的创建。

由于每次请求都需要这些动作,因此我把它加入到了一个过滤器中。

  1. @WebFilter(urlPatterns = "/*")
  2. public class MyFilter implements Filter {
  3. @Override
  4. public void init(FilterConfig filterConfig) throws ServletException {
  5. }
  6. /**
  7. * 在servlet过滤器中触发getSession方法,一次编写,多处使用
  8. */
  9. @Override
  10. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  11. HttpServletRequest request = (HttpServletRequest) servletRequest;
  12. request.getSession(true);
  13. filterChain.doFilter(request, servletResponse);
  14. }
  15.  
  16. @Override
  17. public void destroy() {
  18.  
  19. }
  20. }

3.内嵌服务器支持

  1. ServletFilterListener可以直接通过@WebServlet@WebFilter@WebListener注解自动注册,无需其他代码。唯一需要的就是在项目的入口类中加一个注解@ServletComponentScan,顾名思义,它会扫描所有的
  1. @WebServlet@WebFilter@WebListener完成对象的注入。
  1. @SpringBootApplication
  2. @ServletComponentScan
  3. public class DataboardApplication {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(DataboardApplication.class, args);
  7. }
  8. }

到此为止,简洁明了,不废话,本地测试成功,直接上tomcat服务器。

二、独立tomcat服务器

最大的坑来了,在本地跑的很正常的程序,在服务器上跑不动,最奇怪的是有些接口能用,有些不能用,经过多方观察,发现用到servlet、listener和filter的都挂了。

搜了很多资料之后,还是在spring boot 2.01的文档中找到这么一句话:

  1. Tip
  2. @ServletComponentScan has no effect in a standalone container, where the containers builtin
  3. discovery mechanisms are used instead.

翻译一下就是@ServletComponentScan这个注解在独立tomcat服务器不可用。

只好退求其次,采用@Bean注入的方式

1.对应的监听器代码

  1. @Component
  2. public class MyHttpSessionListener implements HttpSessionListener {
  3. @Autowired
  4. private AppMetaService appMetaService;
  5.  
  6. @Override
  7. public void sessionCreated(HttpSessionEvent httpSessionEvent) {
  8. Map<String, String> keyMap = appMetaService.getKeyMap();
  9. ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
  10. servletContext.setAttribute("keyMap",keyMap);
  11. }
  12.  
  13. @Override
  14. public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
  15. }
  16. }

2.对应过滤器代码

  1. @Component
  2. public class MyFilter implements Filter {
  3. @Override
  4. public void init(FilterConfig filterConfig) throws ServletException {
  5. }
  6. /**
  7. * 在servlet过滤器中触发getSession方法,一次编写,多处使用
  8. */
  9. @Override
  10. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  11. HttpServletRequest request = (HttpServletRequest) servletRequest;
  12. request.getSession(true);
  13. filterChain.doFilter(request, servletResponse);
  14. }
  15.  
  16. @Override
  17. public void destroy() {
  18.  
  19. }
  20. }

3.启动类代码

  1. @SpringBootApplication
  2. public class DataboardApplication {
  3. @Bean
  4. public ServletListenerRegistrationBean sessionHandler() {
  5. return new ServletListenerRegistrationBean<>(new MyHttpSessionListener());
  6. }
  7.  
  8. @Bean
  9. public FilterRegistrationBean myFilter() {
  10. FilterRegistrationBean myFilter = new FilterRegistrationBean(new MyFilter());
  11. myFilter.addUrlPatterns("/*");
  12. return myFilter;
  13. }
  14.  
  15. public static void main(String[] args) {
  16. SpringApplication.run(DataboardApplication.class, args);
  17. }
  18. }

这里变化最大,去掉了@ServletComponentScan,在类内部增加了两个生成bean的方法。

到此,大功告成!

spring boot中使用servlet、listener和filter的更多相关文章

  1. 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  2. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  3. Spring Boot中使用Servlet与Filter

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...

  4. Spring boot中注册Servlet

    Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...

  5. spring boot 中使用servlet

  6. Spring Boot中一个Servlet主动断开连接的方法

    主动断开连接,从而返回结果给客户端,并且能够继续执行剩余代码. 对于一个HttpServletResponse类型的对象response来说,执行如下代码: response.getWriter(). ...

  7. 传统的Servlet在spring boot中怎么实现的?

    传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...

  8. Spring Boot 自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

  9. (7)Spring Boot web开发 --- servlet容器

    文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...

随机推荐

  1. Configuring a remote m-phy

    An interface for low power, high bandwidth communications between units in a device in provided here ...

  2. 用ORACHK自己主动化检查数据库系统的健壮性

    1.orachk工具主要用途 (1)主动检查您的整个软件在操作系统.CRS.数据库.高可用等层面中的严重问题,以便于IT部门整改,提升系统的稳定性 (2)对于您系统中存在的风险提供简单化和合理化的诊断 ...

  3. 用Python模拟浏览器操作

    两种思绪三种要领: 用pamie.建议不要使用,因为pamie为小我私人开发,里面的bug比力多,并且是直接使用win32com体式格局的,如果ie不警惕修改了,后果很严重.另外,pamie3使用的是 ...

  4. ArcGIS Engine 中的多线程使用[转载]

    一直都想写写AE中多线程的使用,但一直苦于没有时间,终于在中秋假期闲了下来.呵呵,闲话不说了,进入正题!         大家都了解到ArcGIS中处理大数据量时速度是相当的慢,这时如果你的程序是单线 ...

  5. iOS 往来--书面资料

    写接触知识和查询功能的基础,现在我们就来看看信息写入 新 变化 删除 #pragma mark - 系人信息 //创建联系人 - (void) creatNewRecord { CFErrorRef ...

  6. 一个JSP结果页面tomcat内存溢出

    如今,试验组的同事寻找新能源我看到一个奇怪的现象.一个tomcat应用,内只有一个简单的jsp页面,和这个jsp无论是什么页java代码(我想用这个jsp在她的网页测试server一对tomcat的最 ...

  7. VisualStateManager

    管理控件状态和管理控件状态的转换逻辑 <Window.Resources> <Style TargetType="Button" x:Key="Anim ...

  8. 更改开机默认不显示explorer.exe,直接启动自己写的EXE程序方法

    原文:更改开机默认不显示explorer.exe,直接启动自己写的EXE程序方法 更改开机默认不显示explorer.exe,直接启动自己写的EXE程序的函数: bool UpdateWinlogon ...

  9. js onload事件使用

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. mysql主从配置及其读写分离

    mysql主从配置意思就是一个主mysql服务器,一个从mysql服务器,一共要用到两台服务器.主服务器新增一个账号专门让从服务器来访问同步工作,主从配置完成后,主服务器主要就是新增和update操作 ...