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

    spring事件类型:ApplicationEvent

    spring事件监听器: ApplicationListener

    spring事件广播器: ApplicationEventMulticaster

    监听所有spring事件:

    public class SpringEventDemo {
        public static void main(String[] args) {
            GenericApplicationContext context = new GenericApplicationContext();
            context.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
                @Override
                public void onApplicationEvent(ApplicationEvent event) {
                    System.err.println(event);
                }
            });
            context.refresh(); //ContextRefreshedEvent
            context.publishEvent("first"); //PayloadApplicationEvent
            context.publishEvent(new MyEvent("myEvent")); //SpringEventDemo$MyEvent
            context.close(); //ContextClosedEvent
        }
    
        private static class MyEvent extends ApplicationEvent{
    
            /**
             * Create a new ApplicationEvent.
             *
             * @param source the object on which the event initially occurred (never {@code null})
             */
            public MyEvent(Object source) {
                super(source);
            }
        }
    
    }

    打印结果:

    org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]
    21:25:20.225 [main] DEBUG org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019
    org.springframework.context.PayloadApplicationEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]
    com.example.springbootdemo.SpringEventDemo$MyEvent[source=myEvent]
    org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]

    监听某个事件:

    public class SpringEventDemo {
        public static void main(String[] args) {
            GenericApplicationContext context = new GenericApplicationContext();
    //        context.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
    //            @Override
    //            public void onApplicationEvent(ApplicationEvent event) {
    //                System.err.println(event);
    //            }
    //        });
            context.addApplicationListener(new MyEventListener());
            context.refresh(); //ContextRefreshedEvent
            context.publishEvent("first"); //PayloadApplicationEvent
            context.publishEvent(new MyEvent("myEvent")); //SpringEventDemo$MyEvent
            context.close(); //ContextClosedEvent
        }
    
        private static class MyEvent extends ApplicationEvent{
    
            /**
             * Create a new ApplicationEvent.
             *
             * @param source the object on which the event initially occurred (never {@code null})
             */
            public MyEvent(Object source) {
                super(source);
            }
        }
    
        private static class MyEventListener implements ApplicationListener<MyEvent>{
    
            @Override
            public void onApplicationEvent(MyEvent event) {
                System.err.println(event);
            }
        }
    
    }

    输出结果:

    com.example.springbootdemo.SpringEventDemo$MyEvent[source=myEvent]

    public class DemoApplication {
    
        public static void main(String[] args) {
            ApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
            multicaster.addApplicationListener(event -> {
                if(event instanceof PayloadApplicationEvent){
                    System.out.println("接收到payloadApplicationEvent");
                }else {
                    System.out.println("接收到事件");
                }
    
            });
    
            multicaster.multicastEvent(new PayloadApplicationEvent<>("1","yin"));
            multicaster.multicastEvent(new MyEvent("myevent"));
        }
    
        private static class MyEvent extends ApplicationEvent{
    
            public MyEvent(Object source) {
                super(source);
            }
        }
    
    }

    输出结果:

    接收到payloadApplicationEvent
    接收到事件

  • 相关阅读:
    一致Hash算法
    Ubuntu14.04上深度学习Caffe库安装指南(CUDA7.5 + opencv3.1)
    springcloud 服务调用的两种方式
    Spring Boot学习记录(二)–thymeleaf模板
    Springmvc构造RESTful详细讲解
    pring MVC过滤器-HttpPutFormContentFilter
    Spring MVC中各个filter的用法
    Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
    springmvc 入门(1)
    oracle sql语言模糊查询--通配符like的使用教程
  • 原文地址:https://www.cnblogs.com/yintingting/p/6574640.html
Copyright © 2011-2022 走看看