Home > Java > javaTutorial > Spring annotation event Event

Spring annotation event Event

高洛峰
Release: 2016-11-22 15:37:02
Original
1557 people have browsed it

Basic support

First let’s take a look at what default events are supported

Spring annotation event Event

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...
    }
}
Copy after login

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;
    }
}
Copy after login

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 == &#39;foo&#39;")    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");
    }
}
Copy after login

@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;
    }
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template