026 Spring event mechanism--basic content

Source: Internet
Author: User

One. Overview

Here we're not going to talk about the benefits of the event mechanism there is a fundamental conceptual problem, and we focus on the Spring container event.

Using the event mechanism, we can accomplish asynchronous method calls, and we can also get concurrency benefits by using the threading mechanism.

Two. Beginning of container Event

Let's take a look at how the container events are published in the spring source code.

In the Refresh () method, we can see the following method when we enter.

//Initialize Event Multicaster for this context.Initapplicationeventmulticaster (); //Initialize Other special beans in specific context subclasses.Onrefresh (); //Check for listener beans and register them.registerlisteners (); //Instantiate all remaining (Non-lazy-init) singletons.finishbeanfactoryinitialization (beanfactory); //Last step:publish corresponding event.Finishrefresh ();

We can see. The Spring container first registers a listener object that applies an event multicast and then registers the event.

Here we know that spring has completed the registration of the publisher and listener of the event.

What we are comparing now is what kind of events spring has released.

protected voidFinishrefresh () {//Initialize Lifecycle Processor for the this context.initlifecycleprocessor (); //Propagate Refresh to Lifecycle processor first.getlifecycleprocessor (). Onrefresh (); //Publish the final event.Publishevent (NewContextrefreshedevent ( This)); //participate in Livebeansview MBean, if active.Livebeansview.registerapplicationcontext ( This); }

Entering into the Finishrefresh () method, we have noticed the Publishevent () method.

This method publishes a container refresh event.

In other words, the IOC container automatically publishes a refreshed event after initialization is complete.

Three. Implement a listener to complete the listener of the refresh event

Spring provides us with a Applicationlistener interface, and our Pojo has the ability to listen to events as long as the interface is implemented.

//Create a listener of your own Public classMyListener implements Applicationlistener<applicationevent>{@Override Public voidOnapplicationevent (applicationeventEvent) {        //Here We listen to all the container events.        if(Eventinstanceof Contextrefreshedevent) {            //a container refresh event is issued at this time            if((applicationcontextevent)Event). Getapplicationcontext (). getParent () = =NULL) {                //Why do we have to make such judgments? Because the event is emitted two times when our SPRINGMVC and spring are integrated. //we need to make sure that the spring container must be completed before the event can respondSystem. out. println ("the container refreshed ...."); }        }    }}

Now that we've opened the container, we'll see if there's any printing information for the container to refresh.

Configuration information:

@Configuration @import (mylistener. class )publicclass  Config {}

Test code:

 Public class maintest {    publicstaticvoid  main (string[] args) {         New Annotationconfigapplicationcontext (Config.  Class);}    }

Four. Concept Description

[1] Event Publisher:

In spring, the publisher of the event is the IOC container, which means that we can publish events as long as we get a reference to the IOC container.

 Public Interface ApplicationContext extends Environmentcapable, listablebeanfactory, Hierarchicalbeanfactory,        Messagesource, applicationeventpublisher, resourcepatternresolver {

The reason is that ApplicationContext implements the interface for event Publishing.

 Public Interface     voidevent)     ;    void Event );}

In this interface, we have a way to publish events.

[2] Event object

In the above we see the Applicationevent object, we first look at its structure.

 Public Abstract class Applicationevent extends EventObject {    Private Long timestamp;     Public applicationevent (Object source) {        super (source);         this. Timestamp = System.currenttimemillis ();    }     Public Long Gettimestamp () {        returnthis. timestamp;    }}

We see that the event object itself has no special place, and that the Spring event object is also a subclass of the JDK's event object.

Another thing we need to note is the object, which represents the source of the event.

[3] Listener for event

is to implement the Applicationlistener object.

Five. Asynchronous

Is our current event asynchronous?

Take a look at the test below.

 Public class maintest {    publicstaticvoid  main (string[] args) {        new Annotationconfigapplicationcontext (Config.  Class);                System. out. println (" initialization of container execution ");}    }

In our test code, add a print statement.

We also need to modify the listener.

//Create a listener of your own Public classMyListener implements Applicationlistener<applicationevent>{@Override Public voidOnapplicationevent (applicationeventEvent) {        //Here We listen to all the container events.        if(Eventinstanceof Contextrefreshedevent) {            //a container refresh event is issued at this time            if((applicationcontextevent)Event). Getapplicationcontext (). getParent () = =NULL) {                //Why do we have to make such judgments? Because the event is emitted two times when our SPRINGMVC and spring are integrated. //we need to make sure that the spring container must be completed before the event can respondSystem. out. println ("the container refreshed ...."); }        }                Try{Thread.Sleep ( the); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

We just added a dormant operation.

When we run the code, we find that the print statement needs to run after the event is complete, which is obviously not asynchronous.

So how can asynchronous operations be implemented?

Very simply, Spring provides us with an asynchronous way to implement it.

@Configuration@EnableAsync@Import (mylistener. class )publicclass  Config {}

First, asynchronous support is turned on.

Then we need to register our listener for asynchronous execution.

@Async  Public class MyListener implements applicationlistener<applicationevent>{

An asynchronous operation can be done with just one annotation.

026 Spring event mechanism--basic content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.