SpringBoot整合Listener

SpringBoot整合Listener有两种方式:

方式一:通过扫描注解完成Listener组件注册

   步骤一:创建一个Listener

package com.shiqp.test.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class FirstListener implements ServletContextListener {
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("listener...");
	}

	public void contextDestroyed(ServletContextEvent sce) {

	}
}

 步骤二:创建一个启动类

package com.shiqp.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class TestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}

}

  步骤三:启动项目

SpringBoot整合Listener

 

 至此,通过扫描注解完成Listener的注册。从上图可知,监听器已生效。

方式二:通过方法完成Listener组件注册

   步骤一:创建一个Listener

package com.shiqp.test.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SecondListener implements ServletContextListener {
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("listener...");
	}

	public void contextDestroyed(ServletContextEvent sce) {

	}
}

  步骤二:创建一个启动类

package com.shiqp.test;

import javax.servlet.ServletContextListener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.shiqp.test.listener.SecondListener;

@SpringBootApplication
public class TestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}

	@Bean
	public ServletListenerRegistrationBean<ServletContextListener> getServletListenerRegistrationBean() {
		ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean<ServletContextListener>(
				new SecondListener());
		return bean;
	}

}

  其余步骤同方式一,启动启动类,出现下图表示监听器已经生效。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2020-12-16 10:26:28.981  INFO 52324 --- [           main] com.shiqp.test.TestApplication           : Starting TestApplication on DESKTOP-H8CM5LI with PID 52324 (E:\resource\workspaces_work\test\target\classes started by shiqi in E:\resource\workspaces_work\test)
2020-12-16 10:26:28.984  INFO 52324 --- [           main] com.shiqp.test.TestApplication           : No active profile set, falling back to default profiles: default
2020-12-16 10:26:29.378  INFO 52324 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-12-16 10:26:29.393  INFO 52324 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12ms. Found 0 repository interfaces.
2020-12-16 10:26:29.778  INFO 52324 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-16 10:26:29.792  INFO 52324 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-16 10:26:29.792  INFO 52324 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2020-12-16 10:26:29.855  INFO 52324 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-16 10:26:29.855  INFO 52324 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 841 ms
listener...
2020-12-16 10:26:30.039  INFO 52324 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-16 10:26:30.241  INFO 52324 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-16 10:26:30.242  INFO 52324 --- [           main] com.shiqp.test.TestApplication           : Started TestApplication in 1.447 seconds (JVM running for 1.996)

 

开发环境:

JDK1.8

SpringBoot2.1.6.RELEASE

 

上一篇:openlayers地图图层与图层特性点击事件


下一篇:领域设计:领域事件