• spring boot 事件监听


    记录工作中的点点滴滴。。。。。。

    maven依赖

        <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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    package com.tsing.event;
    
    import org.springframework.context.ApplicationEvent;
    
    public class MyApplicationEvent extends ApplicationEvent{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public MyApplicationEvent(Object source) {
            super(source);
        }
    
    }

    将监听器纳入到 spring 容器的方式:
    1. 代码: application.addListeners(new MyApplicationListener()); + 实现 ApplicationListener接口
    2. @Component注解 + 实现 ApplicationListener接口
    3. 配置文件的方式 + 实现 ApplicationListener接口
    4. @Component注解 + @EventListener注解

    方式1

    package com.tsing.event;
    
    import org.springframework.context.ApplicationListener;
    
    public class MyApplicationListener implements ApplicationListener<MyApplicationEvent>{
    
        @Override
        public void onApplicationEvent(MyApplicationEvent event) {
            System.out.println("MyApplicationListener接收到事件:" + event.getClass());
        }
    
    }
    package com.tsing;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    import com.tsing.event.MyApplicationEvent;
    import com.tsing.event.MyApplicationListener;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication sp = new SpringApplication(Application.class);
            // 配置事件监听器
            sp.addListeners(new MyApplicationListener());
            
            ConfigurableApplicationContext context = sp.run(args);
            // 发布事件
            context.publishEvent(new MyApplicationEvent(new Object()));
            
            //context.close();
        }
    }

    方式2

    package com.tsing.event;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    /**
     * 将监听器纳入到 spring 容器的方式:
     * 1. 代码: application.addListeners(new MyApplicationListener()); + 实现 ApplicationListener接口
     * 2. @Component注解 + 实现 ApplicationListener接口
     * 3. 配置文件的方式 + 实现 ApplicationListener接口
     * 4. @Component注解 + @EventListener注解
     *
     */
    @Component
    public class MyApplicationListenerByAnnotation implements ApplicationListener<MyApplicationEvent>{
    
        @Override
        public void onApplicationEvent(MyApplicationEvent event) {
            System.out.println("MyApplicationListenerByAnnotation接收到事件:" + event.getClass());
        }
    
    }

    方式3

    package com.tsing.event;
    
    import org.springframework.context.ApplicationListener;
    /**
     * 配置文件实现的方式
     *
     */
    public class MyApplicationListenerByCF implements ApplicationListener<MyApplicationEvent>{
    
        @Override
        public void onApplicationEvent(MyApplicationEvent event) {
            System.out.println("MyApplicationListenerByCF接收到事件:" + event.getClass());
        }
    
    }

    配置文件application.properties

    context.listener.classes=com.tsing.event.MyApplicationListenerByCF

    方式4

    package com.tsing.event;
    
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyEventHandle {
    
        @EventListener
        public void event1(MyApplicationEvent event) {
            System.out.println("事件1==>>" + event.getClass());
        }
    
        @EventListener
        public void event2(MyApplicationEvent event) {
            System.out.println("事件2==>>" + event.getClass());
        }
    
        @EventListener
        public void event3(MyApplicationEvent event) {
            System.out.println("事件3==>>" + event.getClass());
        }
    }

  • 相关阅读:
    【python】学习笔记10-ddt数据驱动
    【python】学习笔记10-装饰器
    【Python】学习笔记8-多线程多进程
    【Python】学习笔记7-异常处理try。。except .. as e ....else
    【Python】学习笔记6-补充Flask模块:登录接口,mysql数据库、存redis-sesson、存浏览器cookie
    【Python】学习笔记6-创建Excel:xlwt,读取Excel:xlrd ,修改Excel:xlutils
    【Python】学习笔记6-网络编程urllib,request,请求rul
    【Python】学习笔记5-利用flask来mock接口
    【Python】学习笔记5-操作redis数据库redis
    【Python】学习笔记5-模块pymysql操作mysql数据库
  • 原文地址:https://www.cnblogs.com/tsing0520/p/12800046.html
Copyright © 2020-2023  润新知