企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
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应用启动了! ```