SpringBoot系列教程web篇Listener四種注冊姿勢


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系列博文

項目源碼

1. 一灰灰Blog

盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激

下面一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛

一灰灰blog


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM