zoukankan      html  css  js  c++  java
  • 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());
        }
    }

  • 相关阅读:
    TMF大数据分析指南 Unleashing Business Value in Big Data(一)
    TMF接口标准MTOSI演进路线图
    移动端开发:使用jQuery Mobile还是Zepto
    拖动插件的一些常见问题
    前端开发资料详解
    AngularJS源码解析3:RootScope的创建过程
    AngularJS源码解析2:注入器的详解
    AngularJS源码解析1:angular自启动过程
    angular核心原理解析3:指令的执行过程
    angular核心原理解析2:注入器的创建和使用
  • 原文地址:https://www.cnblogs.com/tsing0520/p/12800046.html
Copyright © 2011-2022 走看看