Prior to the completion of the IOC parsing and instantiation of XML , the next step was to analyze Spring 's advanced version of the IOC function Extensions for containers:
The code analysis is as follows:
Synchronized (this.startupshutdownmonitor) {//Prepare to refresh Context Preparerefresh ();//Initialize beanfactory and make XML file read Most of the previous IOC's core logic is here configurablelistablebeanfactory beanfactory = Obtainfreshbeanfactory ();// The beanfactory is populated with various functions preparebeanfactory (beanfactory); try {//leave the subclasses with a way to overwrite and extend the service. Postprocessbeanfactory (Beanfactory) ;//activate various beanfactory processors. Invokebeanfactorypostprocessors (beanfactory);//activate Bean post processor, call at Getbean time, The Beanfactory processor is at the container level and is called at this point. Registerbeanpostprocessors (beanfactory);//internationalization of resources processing. Initmessagesource ();//Initialize the broadcaster , used to put all the bean's listeners, and put in the Beanfactory property Applicationeventmulticaster. Initapplicationeventmulticaster ();// Leave the subclass to initialize the other beans, the current method is empty. Onrefresh ();//Find all the listeners of the bean and register it with the broadcaster. Registerlisteners ();// Initializes the remaining single instance. Finishbeanfactoryinitialization (beanfactory);//completes the refresh process, notifies the lifecycle. Finishrefresh ();}
The next step is to start with a one by one explanation of the above steps:
Preparerefresh ();// Prepare to refresh the context environment
protected voidPreparerefresh () {//methods left for subclass overridesinitpropertysources (); //Validate that all properties marked as required is resolvable//Verify that the required properties have been stored in the environmentgetenvironment (). Validaterequiredproperties (); //Allow for the collection of early applicationevents,//To being published once the Multicaster is available ... This. earlyapplicationevents =NewLinkedhashset<applicationevent>(); }
Here is an example to help understand how to verify that the required properties have been stored in the environment
public class Myclasspathxmlapplicationcontext extends Classpathxmlapplicationcontext{public Myclasspathxmlapplicationcontext (String ... configlocations) throws beansexception {super (configlocations);} protected void Initpropertysources () { ///Here Set the environment variable to be detected vargetenvironment (). Setrequiredproperties ("VAR");} protected void Customizebeanfactory (Defaultlistablebeanfactory beanfactory) {super.setallowcircularreferences ( FALSE); super.setallowbeandefinitionoverriding (false); Super.customizebeanfactory (beanfactory);}}
we are using environment variables are validated when the myclasspathxmlapplicationcontext object loads the bean
when using ApplicationContext CTX = new Myclasspathxmlapplicationcontext ("Spring.xml"); , if the environment variable does not add VAR , you will get an error and throw an exception.
Extension of the Spring IOC container