Extension of the Spring IOC container

Source: Internet
Author: User

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

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.