spring event mechanism

Keywords: Spring xml Attribute Lombok

In the process of software development, sometimes we encounter the need to decouple the specific business so that each module is only responsible for the corresponding processing logic. For example, in our abstract business logic, there is a driver responsible for the destination. Now our business system not only deals with the driver to the destination. There are also other business modules that need to focus on the driver's specific destination. However, drivers are only responsible for delivering passengers to specific places. There's no need to tell the business where he's going. At this time, we can use spring event mechanism to decouple business.

spring's event mechanism includes:

Event source, corresponding to specific events.
Event listener is responsible for receiving specific event sources.
Event broadcaster, responsible for publishing event sources.

The corresponding processing logic is that the event listener registers the specific event source. When the event broadcaster broadcasts the specific event source, the event listener can receive the release message of the event, and then process the related events.

Event Source

Defining event sources requires inheriting spring's ApplicationEvent class, which has two subclasses, ApplicationContextEvent and RequestHandledEvent.
_Application ContextEvent has four subclasses, ContextStarted Event, ContextRefreshedEvent, ContextClosed Event, ContextStoppedEvent, which represent the events of container start, refresh, close and stop, respectively.
_RequestHandled Event generates this event only when the Dispatcher Servlet is defined, and its subclass ServletRequestHandled Event represents the Servlet request event.
_Here we choose to define a driver driving event class: DriverEvent, which inherits from ApplicationContextEvent. It has a destination attribute, destination, and here you need to implement its constructor to provide a spring context.

package spring;

import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;

@Data
public class DriveEvent extends ApplicationContextEvent {

    private String destination;
    public DriveEvent(ApplicationContext source, String destination) {
        super(source);
        this.destination = destination;
    }
}

Event listener

We define a DriveEventListener event listener, register the event DriveEvent of its concern through generics, and then implement the onApplication Event (DriveEvent event) of the interface, where we print out the destination of the event.

package spring;

import org.springframework.context.ApplicationListener;

public class DriveEventListener implements ApplicationListener<DriveEvent> {
    @Override
    public void onApplicationEvent(DriveEvent event) {
        System.out.println("This Ha-ha train is bound for its destination:" + event.getDestination());
    }
}

Event Broadcaster

_Next, we need to define event broadcasters. Event broadcasters actually call their publishEvent (Application Event Event Event Event) method through spring's context ApplicationContext. The parameters are the specific event sources inherited from Application Event. Here we define a Driver class, which is responsible for the specific drive. The car goes to its destination and then publishes a Drive Event.

package spring;

import org.springframework.context.ApplicationContext;

public class Driver {
    private ApplicationContext applicationContext = SpingContextHolder.getApplicationContext();
    
	public void drive(String destionation) {
        System.out.println("The driver drove to his destination.");
        DriveEvent driveEvent = new DriveEvent(applicationContext, destionation);
        applicationContext.publishEvent(driveEvent);
    }
}

Here you can see that Driver has a drive(String desctination) method, which is responsible for the specific driving logic, and then he will publish a driving event through spring context. For convenience, we define a SpingContext Holder to get the spring context.
_Getting spring context in a program can either specify a specific xml file or implement the Application Context Aware interface, and then implement its setApplication Context (Application Context ctx) method. Spring passes the context of the predecessor. We define a static context variable CO for this class. Ntext, assign spring's context to it. The code is as follows:

package spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpingContextHolder implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        context = ctx;
    }

    public static ApplicationContext getApplicationContext(){
        return context;
    }
}

Startup class

Now that the corresponding event mechanism components have been defined, let's define a startup class.
_The logic flow of the startup class is: we assemble a driver component driver in spring configuration file context.xml; program startup, get the driver's bean through ClassPathXml Application Context; driver calls drive method to drive to the destination. At this point, your own drive method will publish the DriveEvent event, which DriveEventListner will listen to and print out the destination of the driver. Here the driver only publishes a driving event in the drive method, then drives to the destination. The specific processing logic is handed over to the listener processing logic through the event mechanism, which achieves the effect of program decoupling.

package spring;

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

public class App {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
        Driver driver = (Driver) applicationContext.getBean("driver");
        driver.drive("pvg");
    }
}

spring configuration file:

   <bean id="spingContextHolder" class="spring.SpingContextHolder"/>
   <bean id="driver" class="spring.Driver"/>
   <bean id="driveListener" class="spring.DriveEventListener"/>

summary

We define specific events by inheriting the Application Event class, and then implement Application Listener to register specific events and corresponding processing logic. Event broadcasting is achieved by calling its publishEvent (Application Event Event Event Event Event Event) method in spring context Application Context. When the event broadcaster broadcasts the specific event source, the event listener can receive the publication message of the event and process the related events.

Posted by w00kie on Wed, 07 Aug 2019 00:43:03 -0700