spring-ApplicationContext event mechanism

1, by ApplicationEvent classes and ApplicationListener interface enables event handling the ApplicationContext.

     If there is a ApplicationListener bean container, when the ApplicationContext release ApplicationEvent, ApplicationListener bean will automatically be triggered.

2, two important members of the spring event framework (ie Event, Listener):

     1 "ApplicationEvent: container events, must be issued by the ApplicationContext.

     2 "ApplicationListener: listener, by any listener container bean served.

     3 elements of the mechanism in the event: Event Source (ApplicationContext), event (Event), the event listener (Listener).

     Event Event -> ApplicationContext event source publishing events -> trigger Listener Listener -> Listener performs internal onApplicationEvent (ApplicationEvent e) method, Event event as a parameter passed.

     From the above process, only you need to register to achieve a ApplicationListener of bean in the container, when the ApplicationContext publishing events, the listener will be automatically captured.

     beans.xml register event listeners real example:

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<-! Root element spring configuration file, use the spring-beans-4.0.xsd semantic constraints -> 
< Beans xmlns: xsi = "HTTP: //www.w3.org/2001/XMLSchema-instance " 
       xmlns =" http://www.springframework.org/schema/beans " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
       HTTP : //www.springframework.org/schema/beans/spring-beans-4.0.xsd " > 
      <-! configuration listener -> 
      < the bean class =" com.lfy.listener.EmailNotifier " /> 
</ Beans >

     EmailNotifier.java simple example code ApplicationListener interfaces are as follows:

Package Penalty for com.lfy.listener; 

Import org.springframework.context.ApplicationEvent;
 Import org.springframework.context.ApplicationListener; 

Import com.lfy.event.EmailEvent; 

/ ** 
 * listener class container events 
 * @author LFY 
 * 
 * / 
public  class EmailNotifier the implements the ApplicationListener {
     // this method is triggered automatically when the container is incident 
    @Override
     public  void onApplicationEvent (the ApplicationEvent EVT) {
         // only process EmailEvent, send email notification analog 
        IF (EVT the instanceof EmailEvent) {
            EmailEvent EmailEvent = (EmailEvent) EVT; 
            System.out.println ( "receiver needs to send mail address" + emailEvent.getAddress ()); 
            System.out.println ( "e-mail message body to be sent" + emailEvent.getText () ); 
        } the else {
             // other events without any treatment 
            System.out.println ( "other events:" + evt); 
        } 
    } 
}

    The simple events inherited ApplicationEvent java bean example EmailEvent.java

Package com.lfy.event; 

Import org.springframework.context.ApplicationEvent; 

/ ** 
 * The ApplicationContext event mechanism 
 * a long ApplicationEvent java class inherits the base class, the class object 
 * can be used as a container vessel spring event 
 * @author LFY 
 * 
 * / 
public  class EmailEvent the extends the ApplicationEvent {
    Private String address;
    Private String text; 
   
   public String getAddress () {
        return address; 
   } 
   public  void the setAddress (String address) {
        the this .Address = address;
   } 
   PublicGetText String () {
        return text; 
   } 
   public  void the setText (String text) {
        the this .text = text; 
   } 
   
   public EmailEvent (Source Object) {
        Super (Source); 
   } 
   // constructor initializes all member variables 
   public EmailEvent (Object Source, String address, String text) {
        Super (Source);
        the this .Address = address;
        the this .text = text; 
   } 
   
}

     Simple ApplicationContext event source presentation for publishing events trigger the execution process listener:

     SpringListenerTest.java

package com.lfy.main;

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

import com.lfy.event.EmailEvent;

public class SpringListenerTest {

    public static void main(String[] args) {
        //创建spring容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
        EmailEvent ele=new EmailEvent("test","[email protected]","this is a test");
        //发布容器事件
        ctx.publishEvent(ele);
    }

}

    Results of the:

   Note: The operating results of the above "other events" because when the system creates a spring container, spring loaded container, the container will automatically trigger the destruction of container events (built-in event), container event listener can listen to these events. ApplicationContext of publishEvent (...) for active triggering event specified container Event event.

   If you want the bean container business during the release of a specified event, you should obtain a reference to ApplicationContext let the bean container, and then handed over to the specified container event Event ApplicationContext release. Let spring bean container acquisition

3, built-in event spring provided:

     1 "ContextRefreshedEvent: ApplicationContext container initialization or refresh triggered the event. Here said initialization means that all the bean is successfully loaded, bean post-processing is detected activated, all of the pre-singleton bean is initialized, ApplicationContext container is ready for use.

     2 "ContextStartdEvent: This event is triggered when the start of the sub-interface ConfigurableApplicationContex use ApplicationContext interface () method to start ApplicationContext container. Container bean instance management lifecycle will receive a specified start signal, which restarted after often need to stop the situation is more common.

     3 "ContextClossedEvent: This event is triggered when using ConfigurableApplicationContex interface close () method to close the ApplicationContext container.

     4 "ContextStoppedEvent: This event is triggered when ConfigurableApplicationContex interface stop () method of the container ApplicationContext stopped. "Stop" here means, bean examples of container management lifecycle will receive a specified stop signal. Stopped spring container can reboot method again by calling the start ().

     5 "RequestHandledEvent: event-related Web, can only be applied using DispatcherServlet of Web applications in. When used as a front end of the MVC controller spring, when the spring after the user requests, the system will automatically trigger the event.

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11324173.html