AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
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应用启动了! ```