Springboot's event Release system

Source: Internet
Author: User

For springboot applications, starting a spring container is roughly the following process:

    • The container starts to start
    • Initializing environment variables
    • Initializing context
    • Load context
    • Complete

The listener of the corresponding spring application's initiator can listen to the above procedure, the interface is as follows:

1  Public InterfaceSpringapplicationrunlistener {2 3     /**4 * Called immediately when the Run method has first started. Can is used for very5 * Early initialization.6      */7     voidstarted ();8 9     /**Ten * Called once the environment have been prepared, but before the One      * {@linkApplicationContext} has been created. A      * @paramEnvironment The Environment -      */ -     voidenvironmentprepared (configurableenvironment environment); the  -     /** - * Called once the {@linkApplicationContext} had been created and prepared, but - * Before sources has been loaded. +      * @paramcontext the application context -      */ +     voidcontextprepared (Configurableapplicationcontext context); A  at     /** - * Called once the application context had been loaded but before it had been - * refreshed. -      * @paramcontext the application context -      */ -     voidcontextloaded (Configurableapplicationcontext context); in  -     /** to * called immediately before the Run method finishes. +      * @paramcontext the application context or NULL if a failure occurred before the - * Context was created the      * @paramexception any run exception or null if run completed successfully. *      */ $     voidfinished (configurableapplicationcontext context, throwable exception);Panax Notoginseng  -}
Listener

Corresponding to several key events

Each method in the listener interface represents a stage in the container initiation process, and each stage can be customized to perform a series of tasks. Springapplicationrunlistener can be understood as a command pattern (passing in an object, as to the current point in time, how to call, not caring). Springapplicationrunlistener has a special implementation of Eventpublishingrunlistener, his main function is to start a special event at a specific point in time.

@Override Public voidstarted () { This. Initialmulticaster.multicastevent (NewApplicationstartedevent ( This. Application, This. args)); } @Override Public voidenvironmentprepared (configurableenvironment environment) { This. Initialmulticaster.multicastevent (NewApplicationenvironmentpreparedevent ( This. Application, This. args, environment)); }

Springapplicationrunlisteners is a proxy for multiple springapplicationrunlistener, aggregating multiple springapplicationrunlistener, completing in either process, The corresponding method is called in turn.

    Private FinalList<springapplicationrunlistener>listeners; Springapplicationrunlisteners (log log, Collection<?extendsSpringapplicationrunlistener>listeners) {         This. log =log;  This. Listeners =NewArraylist<springapplicationrunlistener>(listeners); }     Public voidstarted () { for(Springapplicationrunlistener Listener: This. Listeners)        {listener.started (); }    }     Public voidenvironmentprepared (configurableenvironment environment) { for(Springapplicationrunlistener Listener: This. Listeners)        {listener.environmentprepared (Environment); }    }

Event release with Observer pattern

Springboot's event Release system

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.