Spring 注解事件Event

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这个方法会自动帮我们调用,拿到广播发送者

1/** 2 * @author Carl 3 * @date 2016/8/28 4 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 5 */ 6public class EmailService implements ApplicationEventPublisherAware { 7 8 private List<String> blackList; 9 private ApplicationEventPublisher publisher; 10 11 public void setBlackList(List<String> blackList) { 12 this.blackList = blackList; 13 } 14 15 public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { 16 this.publisher = publisher; 17 } 18 19 /** 20 * 具体广播类 21 * @param address 22 * @param text 23 */ 24 public void sendEmail(String address, String text) { 25 if (blackList.contains(address)) { 26 BlackListEvent event = new BlackListEvent(this, address, text); 27 publisher.publishEvent(event); 28 return; 29 } 30 // send email... 31 } 32}

程序2(Event)

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

1/** 2 * @author Carl 3 * @date 2016/8/28 4 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 5 */ 6public class BlackListEvent extends ApplicationEvent { 7 private String address; 8 private String test; 9 10 public BlackListEvent(Object source, String address, String test) { 11 super(source); 12 this.address = address; 13 this.test = test; 14 } 15 16 public String getAddress() { 17 return address; 18 } 19 20 public void setAddress(String address) { 21 this.address = address; 22 } 23 24 public String getTest() { 25 return test; 26 } 27 28 public void setTest(String test) { 29 this.test = test; 30 } 31}

程序3(receiver)

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

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

1/** 2 * @author Carl 3 * @date 2016/8/28 4 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 5 */ 6public class BlackListNotifier implements ApplicationListener<BlackListEvent> { 7 8 private String notificationAddress; 9 10 public void setNotificationAddress(String notificationAddress) { 11 this.notificationAddress = notificationAddress; 12 } 13 14 @EventListener 15 public void onApplicationEvent(BlackListEvent event) { 16 // notify appropriate parties via notificationAddress... 17 System.out.println("onApplicationEvent, some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); 18 } 19 20 @EventListener(condition = "#event.test == 'foo'") 21 public void onApplicationCustomerEvent(BlackListEvent event) { 22 System.out.println("onApplicationCustomerEvent,some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); 23 // notify appropriate parties via notificationAddress... 24 } 25 26 @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class}) 27 public void handleContextStart() { 28 System.out.println("-------------handleContextStart"); 29 30 } 31 32 /** 33 * 参数可以给BlackListEvent 可以不给 34 */ 35 @EventListener(classes = {BlackListEvent.class}) 36 public void handleBlackListEvent() { 37 System.out.println("-------------handleBlackListEvent"); 38 } 39}

@EventListener

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

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

程序4(config bean)

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

1/** 2 * 配置 3 * @author Carl 4 * @date 2016/8/28 5 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 6 */ 7@Configuration 8public class AppConfig { 9 @Bean 10 public EmailService emailService() { 11 EmailService s = new EmailService(); 12 List<String> emails = new ArrayList<>(3); 13 emails.add("known.spammer@example.org"); 14 emails.add("known.hacker@example.org"); 15 emails.add("john.doe@example.org"); 16 s.setBlackList(emails); 17 return s; 18 } 19 20 @Bean 21 public BlackListNotifier notifier() { 22 BlackListNotifier notifier = new BlackListNotifier(); 23 notifier.setNotificationAddress("blacklist@example.org"); 24 return notifier; 25 } 26} 27

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )