在项目中有时候会遇到全局监听的需求,而全局性的监听该如何配置,代码如下:

package com.demo.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ApplicationListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("*************");
        System.out.println("程序已销毁");
        System.out.println("*************");
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("*************");
        System.out.println("程序已启动");
        System.out.println("*************");
    }

}

这里我只是执行项目启动与销毁时的打印操作,具体业务处理大家可以根据自己的需求进行配置

注意:不要忘了在web.xml中声明,代码如下:

  <!-- 全局监听器 -->
  <listener>
      <listener-class>com.demo.listener.ApplicationListener</listener-class>
  </listener>

 

相关文章:

  • 2021-05-16
  • 2021-07-13
  • 2021-07-20
  • 2022-01-01
猜你喜欢
  • 2021-07-23
  • 2021-05-14
  • 2021-05-11
  • 2021-07-19
  • 2022-12-23
  • 2021-09-13
相关资源
相似解决方案