• (010)Spring Boot之事件监听


      本篇记录一下springboot监听事件的四种方式。总体来说,springboot监听事件包含四个步骤

      (1)自定义要监听的事件

      (2)自定义监听器

      (3)使spring容器获取到监听器,这里有三种方式,后面会依次说到

      (4)发布事件

      下面是详细步骤:

      pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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.edu.spring</groupId>
        <artifactId>springboot</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
    
        <name>springboot</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
        
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.1.6.RELEASE</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    View Code

      MyApplicationEvent.java自定义事件,继承ApplicationEvent

    package com.edu.spring.springboot;
    
    import org.springframework.context.ApplicationEvent;
    
    public class MyApplicationEvent extends ApplicationEvent {
    
        private static final long serialVersionUID = 1L;
    
        public MyApplicationEvent(Object source) {
            super(source);
        }
    }
    View Code

      MyAppilcationListener.java实现接口ApplicationListener,接收自定义事件的泛型

    package com.edu.spring.springboot;
    
    import org.springframework.context.ApplicationListener;
    
    public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {
    
        public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接收到事件:"+event.getClass());    
        }
    
    }
    View Code

      App.java发布事件,这里添加监听器的方式是SpringApplication的addListeners方法

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication app=new SpringApplication(App.class);
            app.addListeners(new MyAppilcationListener());//添加监听
            ConfigurableApplicationContext context=app.run(args);
            context.publishEvent(new MyApplicationEvent(new Object()));//发布事件
            context.close();
        }
    }
    View Code

      运行结果如下:

       第二种添加监听器的方式,在自定义的监听器上添加@Component,如下:

      MyAppilcationListener.java

    package com.edu.spring.springboot;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {
    
        public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接收到事件:"+event.getClass());    
        }
    
    }
    View Code

      App.java,现在不需要调用SpringApplication的addListeners方法,如下:

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication app=new SpringApplication(App.class);
            ConfigurableApplicationContext context=app.run(args);
            context.publishEvent(new MyApplicationEvent(new Object()));//发布事件
            context.close();
        }
    }
    View Code

      第三种添加监听器的方式,在application.properties配置文件中添加context.listener.classes属性,如下

      application.properties

    context.listener.classes=com.edu.spring.springboot.MyAppilcationListener
    View Code

      MyAppilcationListener.java,不需要添加注解,如下:

    package com.edu.spring.springboot;
    
    import org.springframework.context.ApplicationListener;
    
    public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {
    
        public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接收到事件:"+event.getClass());    
        }
    
    }
    View Code

      App.java,现在不需要调用SpringApplication的addListeners方法,如下:

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication app=new SpringApplication(App.class);
            ConfigurableApplicationContext context=app.run(args);
            context.publishEvent(new MyApplicationEvent(new Object()));//发布事件
            context.close();
        }
    }
    View Code

      运行结果如下:

      第四种添加监听器的方式,使用@EventListener注解,如下:

      MyEventHandle.java

    package com.edu.spring.springboot;
    
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyEventHandle {
    
        @EventListener
        public void event(MyApplicationEvent event) {
            System.out.println("MyEventHandle接收到事件:"+event.getClass());    
        }
    }
    View Code

      App.java

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication app=new SpringApplication(App.class);
            ConfigurableApplicationContext context=app.run(args);
            context.publishEvent(new MyApplicationEvent(new Object()));//发布事件
            context.close();
        }
    }
    View Code

      运行结果如下:

       很明显,第四种方式最简洁,既不用实现ApplicationListener接口,也不用调用SpringApplication的addListeners方法。

      spring自定义了许多事件,如下图:

       使用这些事件的时候不用发布,下面拿ContextStoppedEvent举例,该事件在调用ConfigurableApplicationContext的stop方法时触发。

      MyEventHandle.java,可以写多个事件,只要在方法上添加@EventListener即可

    package com.edu.spring.springboot;
    
    import org.springframework.context.event.ContextStoppedEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyEventHandle {
        
        @EventListener
        public void test(ContextStoppedEvent event){
            System.out.println("ContextStoppedEvent接收到事件:"+event.getClass());
        }
        
    }
    View Code

      App.java

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication app=new SpringApplication(App.class);
            ConfigurableApplicationContext context=app.run(args);
            context.stop();
        }
    }
    View Code

      运行结果如下:

  • 相关阅读:
    2.如何安装vmvare tools
    1.如何安装ubuntu
    14.如何读取配置文件的键值对
    android app调试没问题,但打包签名的apk,运行时出现闪退怎么办?
    如何使用jedis进行发布订阅
    如何使用mybatis对mysql数据库进行操作,batis的增删改查
    如何解释json的字符串
    redis可视化工具redisClient
    如何连接远程redis,并且选择某个库进行操作
    理解RESTful架构
  • 原文地址:https://www.cnblogs.com/javasl/p/11827842.html
Copyright © 2020-2023  润新知