zoukankan      html  css  js  c++  java
  • ApplicationListener监听器



     

    public class EmailEvent extends ApplicationEvent {
        private String address;
        private String text;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public EmailEvent(Object source, String address, String text) {
            super(source);
            this.address = address;
            this.text = text;
        }
    
        public EmailEvent(Object source) {
            super(source);
        }
    
    //......address和text的setter、getter
    
    }
    
    
    
    //容器刷新监听
    @Component
    public class TestApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
        Logger log = LoggerFactory.getLogger(TestApplicationListener.class);
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            log.info(contextRefreshedEvent.toString());
            log.info("TestApplicationListener..................................");
        }
    }
    
    
    @Component
    //public class EmailNotifier implements ApplicationListener {
    public class EmailNotifier implements ApplicationListener<EmailEvent> {
        Logger log = LoggerFactory.getLogger(EmailNotifier.class);
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            //if (event instanceof EmailEvent) {
                EmailEvent emailEvent = (EmailEvent) event;
                log.info("邮件地址:" + emailEvent.getAddress());
                log.info("邮件内容:" + emailEvent.getText());
            //} else {
            //    log.info("容器本身事件:" + event);
            //}
        }
    
    
    
    //注解方式
    @Component
    public class EmailNotifier {
        Logger log = LoggerFactory.getLogger(EmailNotifier.class);
    
      @EventListener(EmailEvent.class)
        public void onApplicationEvent(ApplicationEvent event) {
                EmailEvent emailEvent = (EmailEvent) event;
                log.info("邮件地址:" + emailEvent.getAddress());
                log.info("邮件内容:" + emailEvent.getText());
        }
    @SpringBootTest(classes = Springapplication22.class)    //springboot启动类,依靠启动类找扫描springcontext的bean
    @RunWith(SpringRunner.class)
    public class ListenerTest {
        @Autowired
        ApplicationContext context;
    
        @Test
        public void test1() {
            //创建一个ApplicationEvent对象
            EmailEvent event = new EmailEvent("hello", "abc@163.com", "This is a test");
            //主动触发该事件
            context.publishEvent(event);
        }
    }

     贴个主要的源码

    Spring 中事件发布都是通过SimpleApplicationEventMulticaster来实现的

    public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
    		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
    		for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
    			Executor executor = getTaskExecutor();
    			if (executor != null) {
                                    // 异步
    				executor.execute(() -> invokeListener(listener, event));
    			}
    			else {
    				invokeListener(listener, event);
    			}
    		}
    	}

    可以看出,如果设置了Executor则异步发送,否则同步;而且可以看出通过 resolveDefaultEventType(event) 对发布的事件类型进行了校验,这就是为什么我们可以直接使用泛型来指定我们想接收的事件对象, 比如上面的ApplicationListener<MyApplicationEvent>。

    也就是说发布事件event后,会找到对应事件的event的listener,发出通知。

    如果没有加泛型,事件发生都会得到通知。

    Spring内建事件

      • ContextRefreshedEvent: Spring应用上下文就绪事件;
      • ContextStartedEvent: Spring应用上下文启动事件;
      • ContextStopedEvent: Spring应用上下文停止事件;
      • ContextClosedEvent: Spring应用上下文关闭事件;

    参考:https://zhuanlan.zhihu.com/p/145927110

  • 相关阅读:
    为 TortoiseGit 添加 ssh key---运行 TortoiseGit 开始菜单中的 Pageant 程序将ppk私钥加入即可
    公共wifi下的中间人攻击
    漏洞扫描原理——将主机扫描、端口扫描以及OS扫描、脆弱点扫描都统一放到了一起
    网络扫描——非常不错的文章,主要分为端口扫描(确定开放服务)和主机扫描(确定机器存活)
    网络安全类别划分——网络信息采集(端口扫描、漏洞扫描、网络窃听)、拒绝服务攻击、漏洞攻击
    网络欺骗——网络欺骗就是使攻击者可以相信网络信息系统存在有价值的、可利用的安全弱点 蜜罐等
    深度学习调参技巧总结
    深度学习网络调参技巧
    如何选择神经网络的超参数
    深度学习调参策略(二)
  • 原文地址:https://www.cnblogs.com/chenfx/p/15092475.html
Copyright © 2011-2022 走看看