2019独角兽企业重金招聘Python工程师标准>>>

String event

从Spring的4.2版本后,开始支持注解来进行事件广播接收,这使得我们非常方便 当然了Spring也支持JMS消息中间件,这个就可以做多个系统集成了,感觉有点偏题了,先看看事件怎么通过注解来开发


基础支持

先来看看支持哪些默认事件

Event 描述
ContextRefreshedEvent ApplicationContext或者叫spring被初始化或者刷新initialized会触发该事件
ContextStartedEvent spring初始化完,时触发
ContextStoppedEvent spring停止后触发,一个停止了的动作,可以通过start()方法从新启动
ContextClosedEvent spring关闭,所有bean都被destroyed掉了,这个时候不能被刷新,或者从新启动了
RequestHandledEvent 请求经过DispatcherServlet时被触发,在request完成之后

程序1(Service)

先看看程序

ApplicationEventPublisher这个是spring的东西,需要注入来进行发送 因为实现了ApplicationEventPublisherAware所以setApplicationEventPublisher这个方法会自动帮我们调用,拿到广播发送者

/*** @author Carl* @date 2016/8/28* @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司*/
public class EmailService implements ApplicationEventPublisherAware {private List<String> blackList;private ApplicationEventPublisher publisher;public void setBlackList(List<String> blackList) {this.blackList = blackList;}public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {this.publisher = publisher;}/*** 具体广播类* @param address* @param text*/public void sendEmail(String address, String text) {if (blackList.contains(address)) {BlackListEvent event = new BlackListEvent(this, address, text);publisher.publishEvent(event);return;}// send email...}
}

程序2(Event)

这里也是需要继承ApplicationEvent,并且里面可以实现自己的一些必要参数等等,让在收到广播时进行获取,当然通过source也可以的

/*** @author Carl* @date 2016/8/28* @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司*/
public class BlackListEvent extends ApplicationEvent {private String address;private String test;public BlackListEvent(Object source, String address, String test) {super(source);this.address = address;this.test = test;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getTest() {return test;}public void setTest(String test) {this.test = test;}
}

程序3(receiver)

用spring还是得遵循他一套规范,那么接收者的,还得实现ApplicationListener接口,那么所有收到泛型广播的对象,都会转发onApplicationEvent接口里面来的

当然了spring想得很周全,不一定通过实现ApplicationListener这个类,在bean类里面加入注解@EventListener

/*** @author Carl* @date 2016/8/28* @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司*/
public class BlackListNotifier implements ApplicationListener<BlackListEvent> {private String notificationAddress;public void setNotificationAddress(String notificationAddress) {this.notificationAddress = notificationAddress;}@EventListenerpublic void onApplicationEvent(BlackListEvent event) {// notify appropriate parties via notificationAddress...System.out.println("onApplicationEvent, some thing I receive:" + event.getAddress() + ",text:" + event.getTest());}@EventListener(condition = "#event.test == 'foo'")public void onApplicationCustomerEvent(BlackListEvent event) {System.out.println("onApplicationCustomerEvent,some thing I receive:" + event.getAddress() + ",text:" + event.getTest());// notify appropriate parties via notificationAddress...}@EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})public void handleContextStart() {System.out.println("-------------handleContextStart");}/*** 参数可以给BlackListEvent 可以不给*/@EventListener(classes = {BlackListEvent.class})public void handleBlackListEvent() {System.out.println("-------------handleBlackListEvent");}
}

@EventListener

解析一下这个注解怎么用,犹如上面的程序,除了实现接口外,可以通过@EventListener注解来实现

  • condition可以使用SpEL表达式,就是当满足条件才执行
  • classes当触发event对象是这个class才会被执行

程序4(config bean)

这里主要对一些服务以及接受广播bean的注册,以便接受

/*** 配置* @author Carl* @date 2016/8/28* @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司*/
@Configuration
public class AppConfig {@Beanpublic EmailService emailService() {EmailService s = new EmailService();List<String> emails = new ArrayList<>(3);emails.add("known.spammer@example.org");emails.add("known.hacker@example.org");emails.add("john.doe@example.org");s.setBlackList(emails);return s;}@Beanpublic BlackListNotifier notifier() {BlackListNotifier notifier = new BlackListNotifier();notifier.setNotificationAddress("blacklist@example.org");return notifier;}
}

个人学习记录说得不对麻烦大家谅解,或进行评论补充

转载于:https://my.oschina.net/u/1983041/blog/738989

Spring 注解事件Event相关推荐

  1. java spring eventbus_Spring 事件:Application Event

    前言 想必你一定为两个 Bean 之间基于耗时事件处理的通知和处理顺序而困扰吧,有困扰没事,不要憋在肚子里,早已经有先驱们发现了痛点并设计出了解决方案--Application Event.(了解 A ...

  2. Spring注解不生效事件

    在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解是不会生效的. 原因: 通过Spring注解标识的方法,在Spring加载类的时候会生成代理类,通过 ...

  3. Spring5参考指南:事件Event

    文章目录 基于继承的Event 基于注解的Event 异步侦听器 Spring提供了很方便的事件的处理机制,包括事件类ApplicationEvent和事件监听类ApplicationListener ...

  4. Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  5. spring的事件机制实战

    理论 在分布式场景下,实现同步转异步的方式有三种方式: 1.异步线程池执行:比如借助@Asyn注解,放到spring自带的线程池中去执行: 2.放到消息队列中,在消费者的代码中异步的消费,执行相关的逻 ...

  6. 框架源码专题:Spring的事件监听、发布机制 ApplicationListener

    文章目录 1.Spring内置事件 2.自定义事件 3.事件监听器 4.事件发布 publishEvent 4.Spring事件原理 5. 面试题:怎么样可以在所有Bean创建完后做扩展代码? 6. ...

  7. Spring注解开发学习笔记

    1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...

  8. spring注解及扩展

    1,spring配置注解 spring建议通过注解配置,替代原xml配置方式. 使用配置类替代xml配置的优势大体: 1,xml配置维护容易出错而且不易检查,java配置类基于java语法检查,对于j ...

  9. SPRING注解驱动开发-雷神课程超详细笔记

    SPRING注解驱动开发-雷神课程超详细笔记 时间:2021-03-21 2022-04-06更新:最近翻起一年多前写的笔记复习,还是收获颇多,很多当时无法理解的知识现在慢慢能理解了,可能是工作一年的 ...

最新文章

  1. 高精度运算(C++实现)
  2. python中cursor属性_Python – AttributeError:’NoneType’对象没有属性’cursor’
  3. python申请内存函数_Python Ctypes c函数的内存分配
  4. mysql 表设计 date_mysql 表 Date类型
  5. HTML5 history新特性pushState、replaceState,popstate
  6. php 跳转到另外一个php,PHP: 其他变更 - Manual
  7. Python:SQLMap的工作流程
  8. simpledateformat格式_为什么日期格式化时必须有使用y表示年,而不能用Y?
  9. 董承非: 如何从各种类型的错误中学习
  10. 如何编写系统设计说明书
  11. PDM系统与PLM系统
  12. eterm协议指令解析
  13. 互联网创业公司黑话指南 | 嘿嘿嘿……
  14. JS将秒数换算成具体的天时分秒
  15. 闲置的华为悦盒搭建海思NASUbuntu系统(二)
  16. 为什么要学习 Linux?
  17. ZYNQ 千兆以太网 学习
  18. App实战:夜间模式实现方法一
  19. 六大维度让你彻底明白机器视觉与计算机视觉的区别!
  20. 个人站长的出路在哪儿?

热门文章

  1. 利用python os模块批量修改文件名称
  2. 2.Excel 自动匹配学生班级
  3. android+锁屏代码+下载,安卓客户端开发的锁屏源码demo下载,可直接应用到APP中
  4. WinMount,一个改变电脑使用方式的软件
  5. 为什么单相电机要用电容,三相电机不需要电容?
  6. 《DBA的思想天空》读书笔记
  7. TAGNN: Target Attentive Graph Neural Networks for Session-based Recommendation论文阅读笔记
  8. java:听歌姿势~~o(*^@^*)o之酷我
  9. RPA之家手把手带你入门Blue Prism教程系列3_如何新建用户和配置数据库
  10. 超级玛丽workshop-第二周