zoukankan      html  css  js  c++  java
  • spring事件

    DemoEvent 自定义事件 继承ApplicationEvent

    /**
     * 自定义事件
     */
    public class DemoEvent extends ApplicationEvent {
    
        private String msg;
        private String userName;
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public DemoEvent(Object source, String msg, String userName) {
            super(source);
            this.msg = msg;
            this.userName  = userName;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
    }

    DemoListener 监听 实现ApplicationListener 接口的onApplicationEvent 方法

     @Async
        public void onApplicationEvent(DemoEvent event) {
            //模拟业务耗时
            try {
                System.out.println("ThreadName:"+Thread.currentThread().getName());
                System.out.println("消息:"+event.getMsg()+"发送给了"+event.getUserName());
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    DemoPublisher 发布 实现ApplicationEventPublisherAware 接口 的setApplicationEventPublisher 方法 并提供发布方法

    public class DemoPublisher implements ApplicationEventPublisherAware{
    
        public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
            this.applicationEventPublisher = applicationEventPublisher;
        }
    
        private ApplicationEventPublisher applicationEventPublisher;
    
        public  void publish(String msg,String userName){
            applicationEventPublisher.publishEvent(new DemoEvent(this,msg,userName));
        }
    }
    

      App 类

    public class App {
        
        private static ClassPathXmlApplicationContext applicationContext;
    
        public static void main(String[] args) {
            applicationContext = new ClassPathXmlApplicationContext("event/beans.xml");
            final DemoPublisher publisher = (DemoPublisher)applicationContext.getBean("demoPublisher");
            publisher.publish("消息1","张三");
            publisher.publish("消息2","张三");
            publisher.publish("消息3","李四");   
            Thread t1 = new Thread(){
                public void run(){
                    publisher.publish("消息11","王五");
                }
            };
            Thread t2 = new Thread(){
                public void run(){
                    publisher.publish("消息22","二麻子");
                }
            };
            t1.start();
            t2.start();
            System.out.println("主线程"+Thread.currentThread().getName()+"结束");
            
        }
    }

    beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="demoListener" class="event.DemoListener"></bean>
    
        <bean id="demoPublisher" class="event.DemoPublisher"></bean>
    </beans>

    测试结果

    ThreadName:main
    消息:消息1发送给了张三
    ThreadName:main
    消息:消息2发送给了张三
    ThreadName:main
    消息:消息3发送给了李四
    主线程main结束
    ThreadName:Thread-0
    消息:消息11发送给了王五
    ThreadName:Thread-1
    消息:消息22发送给了二麻子

    异步执行了发送消息

  • 相关阅读:
    网盘无法单独同步某个文件的解决方法
    编译cubieboard android 源码过程详解之(七):lichee build
    cb-A10系统优化之(一):去除自启动软件
    ubuntu 使用
    JS——数组中push对象,覆盖问题,每次都创建一个新的对象
    Node.js中npm常用命令大全
    Vue style里面使用@import引入外部css, 作用域是全局的解决方案
    5大浏览器内核和主要代表
    IE调试网页之三:使用 F12 工具控制台查看错误和状态 (Windows)
    div拖拽到iframe上方 导致 缩放和拖拽的不平滑和鼠标事件未放开 解决方法
  • 原文地址:https://www.cnblogs.com/diaobiyong/p/9967945.html
Copyright © 2011-2022 走看看