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());
        }
    }

  • 相关阅读:
    Mybatis主配置文件常见使用讲解
    Mybatis导入原生配置文件
    分页查询时,使用cookie保存上次的查询条件。jQuery实现方法以及中间遇到的坑
    jQuery实现checkbox即点即改,批量计数,以及中间遇到的坑
    ()IT 职场经验)一位10年Java工作经验的架构师的经验分享,感觉很受用。
    SpringMVC的Controller
    Spring MVC执行原理和基于Java的配置过程
    集成学习综述笔记
    generalization error
    Failed to install 'unknown package' from GitHub: schannel: failed to receive handshake, SSL/TLS connection failed
  • 原文地址:https://www.cnblogs.com/tsing0520/p/12800046.html
Copyright © 2011-2022 走看看