![Spring Boot使用监听器Listener Spring Boot使用监听器Listener](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
之前介绍了在Spring Boot中使用过滤器:https://www.cnblogs.com/zifeiy/p/9911056.html
接下来介绍使用监听器Listener。
下面是一个例子:
package com.zifeiy.test.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class TestUserListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContextListener.super.contextInitialized(sce);
System.out.println("ServletContext上下文初始化!");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContextListener.super.contextDestroyed(sce);
System.out.println("ServletContext上下文销毁!");
}
}
在程序启动的时候,可以看到初始化函数调用的结果:
ServletContext上下文初始化!