1.创建maven项目,导入springboot的maven依赖
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>per.czt.04-spring-boot-listener</groupId>
<artifactId>04-spring-boot-listener</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>04-spring-boot-listener</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring-boot启动器 -->
<!-- spring-boot就是一些jar包的集合,一共44个启动器 -->
<!-- spring-boot-start-web,支持全栈开发,包括tomcat和springmvc等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
方式一:通过注解扫描方式整合listener
FirstListener.Class
package per.czt.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/*
* <listener>
* <listener-class>per.czt.listener.FirstListener</listener-class>
*
*
* </listener>
*
*
*/
@WebListener
public class FirstListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
System.out.println("FirstListener init...");
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
SecondListener.Class
启动类:App.Class
package per.czt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* Hello world!
*
*/
@SpringBootApplication
@ServletComponentScan
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
方式二:通过编写方法的方式整合listener
SecondListener.Class
package per.czt.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
System.out.println("SecondListener init...");
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
启动类:App2.Class
package per.czt;
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 per.czt.listener.SecondListener;
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> listener=new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return listener;
}
}