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
个人学习记录说得不对麻烦大家谅解,或进行评论补充