java web三要素Filter, Servlet前面分別進行了介紹,接下來我們看一下Listener的相關知識點,本篇博文主要內容為SpringBoot環境下,如何自定義Listener並注冊到spring容器
I. 環境配置
1. 項目搭建
首先我們需要搭建一個web工程,以方便后續的servelt注冊的實例演示,可以通過spring boot官網創建工程,也可以建立一個maven工程,在pom.xml中如下配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
II. Listener注冊
我們這里說到的Listener專指java web相關的監聽器,與Spring本身的Listener並不一樣。在java web中Listener的知識點為servlet規范的那一套,這里不詳細展開。下面主要介紹在SpringBoot中使用Servlet Listener的四種方式
1. WebListener注解
@WebListener
注解為Servlet3+提供的注解,可以標識一個類為Listener,使用姿勢和前面的Listener、Filter並沒有太大的區別
@WebListener
public class AnoContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("@WebListener context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("@WebListener context 銷毀");
}
}
因為WebListener注解不是spring的規范,所以為了識別它,需要在啟動類上添加注解@ServletComponentScan
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
2. 普通bean
第二種使用方式是將Listener當成一個普通的spring bean,spring boot會自動將其包裝為ServletListenerRegistrationBean
對象
@Component
public class BeanContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("bean context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("bean context 銷毀");
}
}
3. ServletListenerRegistrationBean
通過java config來主動將一個普通的Listener對象,塞入ServletListenerRegistrationBean
對象,創建為spring的bean對象
public class ConfigContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("config context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("java context 銷毀");
}
}
上面只是一個普通的類定義,下面的bean創建才是關鍵點
@Bean
public ServletListenerRegistrationBean configContextListener() {
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new ConfigContextListener());
return bean;
}
4. ServletContextInitializer
這里主要是借助在ServletContext上下文創建的實際,主動的向其中添加Filter,Servlet, Listener,從而實現一種主動注冊的效果
public class SelfContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextInitializer context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextInitializer context 銷毀");
}
}
@Component
public class ExtendServletConfigInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(SelfContextListener.class);
}
}
注意ExtendServletConfigInitializer的主動注冊時機,在啟動時添加了這個Listenrer,所以它的優先級會是最高
5. 測試
上面介紹了四種注冊方式,都可以生效,在我們的實際開發中,按需選擇一種即可,不太建議多種方式混合使用;
項目啟動和關閉之后,輸出日志如下
II. 其他
web系列博文
- 191122-SpringBoot系列教程web篇Servlet 注冊的四種姿勢
- 191120-SpringBoot系列教程Web篇之開啟GZIP數據壓縮
- 191018-SpringBoot系列教程web篇之過濾器Filter使用指南擴展篇
- 191016-SpringBoot系列教程web篇之過濾器Filter使用指南
- 191012-SpringBoot系列教程web篇之自定義異常處理HandlerExceptionResolver
- 191010-SpringBoot系列教程web篇之全局異常處理
- 190930-SpringBoot系列教程web篇之404、500異常頁面配置
- 190929-SpringBoot系列教程web篇之重定向
- 190913-SpringBoot系列教程web篇之返回文本、網頁、圖片的操作姿勢
- 190905-SpringBoot系列教程web篇之中文亂碼問題解決
- 190831-SpringBoot系列教程web篇之如何自定義參數解析器
- 190828-SpringBoot系列教程web篇之Post請求參數解析姿勢匯總
- 190824-SpringBoot系列教程web篇之Get請求參數解析姿勢匯總
- 190822-SpringBoot系列教程web篇之Beetl環境搭建
- 190820-SpringBoot系列教程web篇之Thymeleaf環境搭建
- 190816-SpringBoot系列教程web篇之Freemaker環境搭建
- 190421-SpringBoot高級篇WEB之websocket的使用說明
- 190327-Spring-RestTemplate之urlencode參數解析異常全程分析
- 190317-Spring MVC之基於java config無xml配置的web應用構建
- 190316-Spring MVC之基於xml配置的web應用構建
- 190213-SpringBoot文件上傳異常之提示The temporary upload location xxx is not valid
項目源碼
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 項目:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/212-web-listener
1. 一灰灰Blog
盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
下面一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛
- 一灰灰Blog個人博客 https://blog.hhui.top
- 一灰灰Blog-Spring專題博客 http://spring.hhui.top