多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
Servlet 监听器有多种,这里以注册 ServletContextListener 为例,其他的大同小异。 <br/> **1. 实现接口ServletContextListener** ```java public class CustomServletContextListener implements ServletContextListener { /** * 监听服务器初始化 */ public void contextInitialized(ServletContextEvent sce) { System.out.println("CustomServletContextListener -> Web应用启动了!"); } /** * 监听服务器销毁 */ public void contextDestroyed(ServletContextEvent sce) { System.out.println("CustomServletContextListener -> Web应用销毁了!"); } } ``` **2. 注册监听器** ```java @Configuration public class CustomServerConfig{ @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean() { return new ServletListenerRegistrationBean<CustomServletContextListener>(new CustomServletContextListener()); } } ``` **3. 测试,启动项目后将在控制台打印如下信息** (1)启动项目后将在控制台打印如下信息。 ``` CustomServletContextListener -> Web应用启动了! ```