1. 通过注解扫描完成Listener组件的注册(SpringBoot整合Listener方式一)
1.1. 使用maven构建SpringBoot的名叫spring-boot-listener项目
1.2. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjbs</groupId>
<artifactId>spring-boot-listener</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<!-- 修改jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
1.3. 编写FirstListener.java
package com.bjbs1.controller;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* springBoot整合Listener方式一
*
* <listener>
* <listener-class>com.bjsxt1.listener.FirstListener</listener-class>
* </listener>
*/
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("FirstListener...contextInitialized......");
}
}
1.4. @WebListener注解
1.5. 编写App.java启动类
package com.bjbs1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* springBoot整合Listener方式一, SpringBoot启动类
*/
@SpringBootApplication
@ServletComponentScan // 在springBoot启动时会扫描@WebListener, 并将该类实例化
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
1.6. 配置@ServletComponentScan注解, 在SpringBoot启动时会扫描@WebListener, 并将该类实例化
1.7. 运行项目
2. 通过方法完成Listener组件注册(SpringBoot整合Listener方式二)
2.1. 编写SecondListener.java
package com.bjbs2.controller;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* springBoot整合Listener方式二
*/
public class SecondListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("SecondListener...contextInitialized......");
}
}
2.2. 编写App2.java启动类
package com.bjbs2;
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.bjbs2.controller.SecondListener;
/**
* SpringBoot整合Listener方式二, SpringBoot启动类
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册listener
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {
ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
2.3. 使用org.springframework.boot.web.servlet.ServletListenerRegistrationBean类注册Listener
2.4. 运行项目