zoukankan      html  css  js  c++  java
  • 使用ServletContextListener关闭Redisson连接

    •  ServletContextListener 监听器

    在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。

    当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。在 ServletContextListener 接口中定义了处理ServletContextEvent 事件的两个方法。

       /**
         * 当Servlet 容器启动Web 应用时调用该方法。在调用完该方法之后,容器再对Filter 初始化,
         * 并且对那些在Web 应用启动时就需要被初始化的Servlet 进行初始化。
         */
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
    
        }
    
    
        /**
         * 当Servlet 容器终止Web 应用时调用该方法。在调用该方法之前,容器会先销毁所有的Servlet 和Filter 过滤器。
         */
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            // 关闭Reddson的线程池连接
            RedisUtils.shutdown();
        }
    • 监听器介绍

    在JavaWeb被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。

    总共八个监听器,监听域对象相关的操作

    监听器的对象和方法的调用都是由服务器自己调用,不能手动参与。

    域对象相关监听器
    ServletContextListener:Tomcat启动和关闭时调用下面两个方法

    public void contextInitialized(ServletContextEvent evt):ServletContext对象被创建后调用;
    public void contextDestroyed(ServletContextEvent evt):ServletContext对象被销毁前调用;
    HttpSessionListener:开始会话和结束会话时调用下面两个方法

    public void sessionCreated(HttpSessionEvent evt):HttpSession对象被创建后调用;
    public void sessionDestroyed(HttpSessionEvent evt):HttpSession对象被销毁前调用;
    ServletRequestListener:开始请求和结束请求时调用下面两个方法

    public void requestInitiallized(ServletRequestEvent evt):ServletRequest对象被创建后调用;
    public void requestDestroyed(ServletRequestEvent evt):ServletRequest对象被销毁前调用。

    域属性相关监听器

    ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法。

    public void attributeAdded(ServletContextAttributeEvent evt)
    public void attributeRemoved(ServletContextAttributeEvent evt)
    public void attributeReplaced(ServletContextAttributeEvent evt)
    HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法

    public void attributeAdded(HttpSessionBindingEvent evt)
    public void attributeRemoved (HttpSessionBindingEvent evt)
    public void attributeReplaced (HttpSessionBindingEvent evt)
    ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法

    public void attributeAdded(ServletRequestAttributeEvent evt)
    public void attributeRemoved (ServletRequestAttributeEvent evt)
    public void attributeReplaced (ServletRequestAttributeEvent evt)

  • 相关阅读:
    http://rpm.pbone.net/
    Linux基础知识之 系统启动流程
    欧几里得算法及其扩展
    组合数相关
    poj2689 Prime Distance(思维+筛素数)
    一本通1624樱花(数学+唯一分解定理)
    CF33C Wonderful Randomized Sum(贪心+思维)
    HNOI 2008越狱(组合数学(乘法原理)+快速幂)
    唯一分解定理
    UVa 11827 Maximum GCD(gcd+读入技巧)
  • 原文地址:https://www.cnblogs.com/yuhuashang-edward/p/10451491.html
Copyright © 2011-2022 走看看