1、MyServletContextListener 实现(implements) ServletContextListener 创建监听器。
package com.wzy.springbootweb02.servlet;import lombok.extern.slf4j.Slf4j;import javax.servlet.ServletConfig;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@Slf4j@WebListenerpublic class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {log.info("MyServletContextLister监听到项目初始化完成。");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {log.info("MyServletContextLister监听到项目销毁。");}}
2、在启动类上面加上扫描注解 @ServletComponentScan``(basePackages = ``"com.wzy.springbootweb02.servlet"``) 扫描Servlet程序。
package com.wzy.springbootweb02;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;@ServletComponentScan(basePackages = "com.wzy.springbootweb02.servlet")@SpringBootApplicationpublic class SpringbootWeb02Application {public static void main(String[] args) {SpringApplication.run(SpringbootWeb02Application.class, args);}}
