Spring Boot使用监听器Listener

之前介绍了在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上下文初始化!
原文地址:https://www.cnblogs.com/zifeiy/p/9917156.html