Basic support
First let’s take a look at what default events are supported
Program 1 (Service)
Let’s take a look at the program first
ApplicationEventPublisher is a spring thing and needs to be injected for sending. Because ApplicationEventPublisherAware is implemented, setApplicationEventPublisher is The method will automatically be called for us to get the broadcast sender
/** * @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... } }
Program 2 (Event)
Here we also need to inherit ApplicationEvent, and we can implement some of our own necessary parameters, etc., so that we can get it when we receive the broadcast. Of course You can also use 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; } }
Program 3 (receiver)
You still have to follow its set of specifications when using spring. Then the receiver must implement the ApplicationListener interface, so all objects that receive generic broadcasts will forward the onApplicationEvent interface. It comes from inside
Of course spring is very thoughtful and does not necessarily implement the ApplicationListener class. Add the annotation @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
to the bean class. Analyze how to use this annotation, just like the above program, except for the implementation Outside the interface, it can be implemented through the @EventListener annotation.
condition can use SpEL expressions, which are executed when the conditions are met.
classes. When the event object is triggered, this class will be executed.
Program 4 (config bean)
Here Mainly for some services and to accept the registration of broadcast beans in order to accept
/** * 配置 * @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; } }