基礎支援
先來看看支援哪些預設事件
程式1(Service)
先看看程式
ApplicationEventPublisher這個是spring的東西,需要先看看程式
ApplicationEventPublisher這個是嗎方法會自動幫我們調用,拿到廣播發送者
/** * @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; } @EventListener public 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.广州市森锐电子科技有限公司 */@Configurationpublic class AppConfig { @Bean public 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; } @Bean public BlackListNotifier notifier() { BlackListNotifier notifier = new BlackListNotifier(); notifier.setNotificationAddress("blacklist@example.org"); return notifier; } }