转载

191206-SpringBoot系列教程web篇Listener四种注册姿势

javaw 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. 测试

上面介绍了四种注册方式,都可以生效,在我们的实际开发中,按需选择一种即可,不太建议多种方式混合使用;

项目启动和关闭之后,输出日志如下

191206-SpringBoot系列教程web篇Listener四种注册姿势

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

191206-SpringBoot系列教程web篇Listener四种注册姿势

打赏 如果觉得我的文章对您有帮助,请随意打赏。

原文  http://spring.hhui.top/spring-blog/2019/12/06/191206-SpringBoot系列教程web篇Listener四种注册姿势/
正文到此结束
Loading...