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