Spring makes it very easy to implement the EventListener mechanism. In this article, I briefly introduce two postures for interface and annotation @eventlistener.

The event sample

The event object

In Spring, all events need to be derived from ApplicationEvent. A basic MsgEvent is as follows

public class MsgEvent extends ApplicationEvent {
    private String msg;

    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public MsgEvent(Object source, String msg) {
        super(source);
        this.msg = msg;
    }

    @Override
    public String toString(a) {
        return "MsgEvent{" +
                "msg='" + msg + '\' ' +
                '} '; }}Copy the code

Interface consumption

There are two ways to consume events. The declaration of interfaces is mainly to implement the ApplicationListener interface. Notice that the Listener needs to be declared as a Spring bean object

@Service
public class MsgEventListener implements ApplicationListener<MsgEvent> {
    @Override
    public void onApplicationEvent(MsgEvent event) {
        System.out.println("receive msg event: "+ event); }}Copy the code

Annotated consumption

Implementing the interface requires creating a new implementation class. It is easier to simply add an annotation @EventListener to the consuming method

@EventListener(MsgEvent.class)
public void consumer(MsgEvent msgEvent) {
    System.out.println("receive msg event by @anno: " + msgEvent);
}
Copy the code

This annotation supports matching by Event parameter type, i.e. in the example above, @eventListener is added directly to the method without specifying parentheses

Publish event

In Spring, the main way to publish an event is to use the ApplicationContext

@Service
@ComponentScan({"com.java.event"})
public class MsgPublisher implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    // Initiate event notification
    public void publish(String msg) {
        applicationContext.publishEvent(new MsgEvent(this, msg));
    }

    // By way of annotations
    @EventListener(MsgEvent.class)
    public void consumer(MsgEvent msgEvent) {
        System.out.println("receive msg event by @anno: " + msgEvent);
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MsgPublisher.class);
        MsgPublisher msgPublisher = context.getBean(MsgPublisher.class);
        msgPublisher.publish("louzai"); }}Copy the code

Description:

  • @ComponentScan is the Bean used to scan for Spring;
  • I made “annotated consumption” a direct MsgPublisher method, mainly for the sake of laziness. You could also separate the entire class and make consumer() a member method of the class.
  • Main () is used for testing and gets the MsgPublisher Bean directly from the ApplicationContext.

test

Directly execute main() of MsgPublisher and the output is as follows:

receive msg event by @anno: MsgEvent{msg='louzai'}
receive msg event: MsgEvent{msg='louzai'}
Copy the code

Afterword.

I read a blog post by a colleague because I was going to use it for development tomorrow, and then did a quick run.

I haven’t written many articles recently, not because I am lazy, but because I have been making a very important decision in the past two weeks. I will tell you the result when it comes out. When the dust settles, I’ll continue to post 1-2 articles a week.

Welcome everyone to like a lot, more articles, please pay attention to the wechat public number “Lou Zai advanced road”, point attention, do not get lost ~~