Spring event (2) - Custom Event


Spring series of tutorials


In addition to built-in event, Spring can also use custom events.

How to use custom events:

  • Create an event class - Extension ApplicationEventclass, create event class.
  • Create a send class - Send the class get ApplicationEventPublisherinstances send events.
  • Create a class monitor - implement ApplicationListenerthe interface, create a listener class.

Event Class

Event class is used to store event data. Create a simple event category below.

CustomEvent.java

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
 
    public CustomEvent(Object source, String message) {
        super(source);
    }
    
    public String toString() {
        return "我是自定义事件";
    }
}

Send class

Send event object class is created and sent.

To send an event to introduce two methods here:

  1. Use @autowiredannotations inject ApplicationEventPublisherinstance.

CustomEventPublisher.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

public class CustomEventPublisher {

  @Autowired
  private ApplicationEventPublisher publisher;
  
  public void publish() {
    CustomEvent event = new CustomEvent(this);
    publisher.publishEvent(event);
  }
}
  1. Sending class implements ApplicationEventPublisherAwarethe interface, acquired ApplicationEventPublisherinstance.

CustomEventPublisher.java

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class CustomEventPublisher implements ApplicationEventPublisherAware {

  private ApplicationEventPublisher publisher;
  
  // 必须重写这个方法获取ApplicationEventPublisher
  public void setApplicationEventPublisher (ApplicationEventPublisher publisher){
    this.publisher = publisher;
  }
  
  public void publish() {
    CustomEvent event = new CustomEvent(this);
    publisher.publishEvent(event);
  }
}

If you send a class implements ApplicationEventPublisherAwarean interface, send the class must be declared as bean, Spring containers to identify it as an event sender.

<bean id="customEventPublisher" class="CustomEventPublisher"/>

Listener class

Listener class listens for events. Listener class must implement ApplicationListenerthe interface, and is defined as Bean Spring container so that it can be loaded.

beans.xml

<bean id="customEventHandler" class="CustomEventHandler"/>

CustomEventHandler.java

import org.springframework.context.ApplicationListener;

public class CustomEventHandler implements ApplicationListener<CustomEvent> { 
  public void onApplicationEvent(CustomEvent event) {
    System.out.println("收到事件:" + event.toString());
  }
}

run

Test custom event.

Test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    
    CustomEventPublisher publisher = (CustomEventPublisher) context.getBean("customEventPublisher");
    publisher.publish();
  }
}

Guess you like

Origin www.cnblogs.com/jinbuqi/p/10987732.html
Recommended