Spring release mechanism to monitor

First, the source

   (1), ApplicationEvent abstract class

public abstract class ApplicationEvent extends EventObject {

    /** use serialVersionUID from Spring 1.2 for interoperability */
    private static final long serialVersionUID = 7099057708183571937L;

    /** System time when the event happened */
    private final long timestamp;
    /**
     * 创建一个新的ApplicationEvent.*/
    public ApplicationEvent(Object source) {
        super(source);
        this.timestamp = System.currentTimeMillis();
    }
    /**
     * Get time event generated
     */
    public final long getTimestamp() {
        return this.timestamp;
    }
}

   (2), ApplicationListener Interface - an event listener container released.

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

    /**
     * Handle an application event.*/
    void onApplicationEvent(E event);

}

   (3), ApplicationEventPublisher Interface

public interface ApplicationEventPublisher {

    / ** 
     * will notify all registered listeners of the event, these monitors may be spring frame listener, there may be a particular listener. * / 
    Void the publishEvent (the ApplicationEvent Event);

    /**
     * Notify all <strong>matching</strong> listeners registered with this
     * application of an event.*/
    void publishEvent(Object event);

}

Second, using actual examples

 (1), create a class, as a passing event content

      

public class Person {
    private int age;
    private String name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

  (2) create a class that implements the interface ApplicationEvent

@Component
public class Event extends ApplicationEvent {
    public Event(Person source) {
        super(source);
    }
}

(3) create ApplicationListener interface implementation class that implements the listener

@Component
public class MyListener implements ApplicationListener<Event> {
    @Override
    public void onApplicationEvent(Event event) {
        Person person = (Person) event.getSource();
        System.out.println (person.getAge ()); // get the event published 
        System.out.println (event.getTimestamp ()); // get the time of the event posted 
    }
}

  (4) Create Configuration configuration class

@Configuration
@ComponentScan("com.spring.event")
public class EventConfiguration {
    @Bean
    public Person person(){
        Person person = new Person();
        person.setName("haha");
        person.setAge(10);
        return person;
    }

}

(5) to start the vessel, and event publishing in a container

public class Test {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(EventConfiguration.class);
        applicationContext.publishEvent(new Event((Person) applicationContext.getBean("person")));
    }
}

(6), you can get to monitor results

10
1578452283603

Guess you like

Origin www.cnblogs.com/mayang2465/p/12165379.html