6-spring boot -监听器
程序员文章站
2022-03-02 18:33:55
...
需要的jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
实例代码
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class StudyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
System.out.println("StudyListener init...");
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("StudyListener destory...");
}
}
实例化
方式一:
com.study.cxk.listener对应StudyListener所在的包
@ServletComponentScan(basePackages = {"com.study.cxk.filter","com.study.cxk.listener"})
方式二:
@SpringBootApplication
public class StudySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(StudySpringBootApplication.class, args);
}
@Bean
public ServletListenerRegistrationBean<StudyListener> servletRegisterBean(){
return new ServletListenerRegistrationBean<StudyListener>(new StudyListener());
}
}
启动
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-18 12:51:56.154 INFO 7472 --- [ main] c.study.cxk.StudySpringBootApplication : Starting StudySpringBootApplication on DESKTOP-VSPGF1T with PID 7472 (D:\code\studySpringBoot\target\classes started by Administrator in D:\code\studySpringBoot)
2020-07-18 12:51:56.155 INFO 7472 --- [ main] c.study.cxk.StudySpringBootApplication : The following profiles are active: shanghai
2020-07-18 12:51:56.631 INFO 7472 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9789 (http)
2020-07-18 12:51:56.635 INFO 7472 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-18 12:51:56.636 INFO 7472 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-18 12:51:56.675 INFO 7472 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-18 12:51:56.675 INFO 7472 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 496 ms
StudyListener init...
2020-07-18 12:51:56.770 INFO 7472 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-18 12:51:56.800 INFO 7472 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2020-07-18 12:51:56.854 INFO 7472 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9789 (http) with context path ''
2020-07-18 12:51:56.859 INFO 7472 --- [ main] c.study.cxk.StudySpringBootApplication : Started StudySpringBootApplication in 0.908 seconds (JVM running for 1.281)
2020-07-18 12:51:59.036 INFO 7472 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
StudyListener destory...
2020-07-18 12:51:59.148 INFO 7472 --- [on(2)-127.0.0.1] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
监听器的作用
主要作用是:做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等