SPRINGMVC Web. XML configuration SPRINGMVC IOC container initialization

Source: Internet
Author: User

SPRINGMVC IOC Container initialization

First of all, the SPRINGMVC IOC container initialization is somewhat special, and in addition to generating a global spring IOC container in SPRINGMVC, a container is generated for Dispatcherservlet, and the next article is described in detail.

We know that spring uses the main function simply to generate a container, as follows:

public class Maintest {public    static void Main (string[] args) {        ApplicationContext appContext = new Classpathxmla Pplicationcontext ("Cjj/models/beans.xml");        Person P = (person) appcontext.getbean (' person ');}    }

A previous article devoted to the spring IOC container initialization process , "Spring IOC container initialization process Learning", is interesting to see. In this article, it is noted that the spring IOC container initialization process typically takes three steps,

    1. Resource positioning (bean definition file positioning)
    2. Load resource-positioned resources into beandefinition
    3. Registering Beandefiniton in a container

The emphasis in that article is on the bean's loading process and the core process, which can look up the obtainfreshbeanfactory process in the source code, which executes refreshbeanfactory creates a Beanfactory instance ( The default type is Defaultlistablebeanfactory), and Loadbeandefinitions (defaultlistablebeanfactory).

However, the spring container initialization process is much more than that described in the article, and there is an important refresh method that needs to be supplemented. Then this article can be combined with the Web. XML configuration loading to simply illustrate the refresh method execution of those logic.

If you are not familiar with the role of the container initialization Web. XML when the application starts, it is recommended to take a look at the previous article, "Springmvc Web. config Contextloaderlistener", This article describes the role of Contextloaderlistener in spring, knowing that Contextloaderlistener first will be common to a xmlwebapplicationcontext type of IOC container, Then read the configuration file information to initialize, if not write, then the default is/web-inf/applicationcontext.xml, you can consult the source code:

public class Xmlwebapplicationcontext extends Abstractrefreshablewebapplicationcontext {public    static final String default_config_location = "/web-inf/applicationcontext.xml";    public static final String Default_config_location_prefix = "/web-inf/";    public static final String Default_config_location_suffix = ". xml";    ... Slightly

Typically, the Web. XML is configured as follows:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value> Classpath*:spring/applicationcontext-*.xml</param-value></context-param>

For example, if configured as above, the Xmlwebapplicationcontext container will read the information in the Applicationcontext-*.xml configuration file and call the Refresh () method, Since Xmlwebapplicationcontext implements in Configurableapplicationcontext, That is, the Configurableapplicationcontext.refresh () method is implemented, which loads the configuration information into the IOC container.

specific, Xmlwebapplicationcontext inherited from Abstractapplicationcontext, It implements the Refresh method in the Configurableapplicationcontext interface:

public void Refresh () throws Beansexception, illegalstateexception {Object var1 = this.startupshutdownmonitor;        Synchronized (this.startupshutdownmonitor) {This.preparerefresh ();        Configurablelistablebeanfactory beanfactory = This.obtainfreshbeanfactory ();        This.preparebeanfactory (beanfactory);            try {this.postprocessbeanfactory (beanfactory);            This.invokebeanfactorypostprocessors (beanfactory);            This.registerbeanpostprocessors (beanfactory);            This.initmessagesource ();            This.initapplicationeventmulticaster ();            This.onrefresh ();            This.registerlisteners ();            This.finishbeanfactoryinitialization (beanfactory);        This.finishrefresh (); } catch (Beansexception var9) {if (this.logger.isWarnEnabled ()) {This.logger.warn ("Exception en            Countered during context initialization-cancelling refresh attempt: "+ var9); } this.deStroybeans ();            This.cancelrefresh (VAR9);        Throw VAR9;        } finally {this.resetcommoncaches (); }    }}

  

    • Preparerefresh ()

The first step is to set up the spring start event, activate the active state, and initialize the attribute source information, and the third is to verify the necessary attributes.

    • Obtainfreshbeanfactory ()

Create Beanfactory (the procedure is to generate beandefinition for each bean based on XML and register to the generated beanfactory)

    • Preparebeanfactory (Beanfactory)

Register beans and so on.

    • Postprocessbeanfactory (Beanfactory)

Follow up after the previous step beanfactory setup

    • Invokebeanfactorypostprocessors (Beanfactory)

    • Registerbeanpostprocessors (Beanfactory)

Instantiate and register a bean that extends beanpostprocessor in beanfactory

    • Initmessagesource ()

Initializes the internationalization attribute.

    • Initapplicationeventmulticaster

Initializes the event broadcaster, which is used to publish events.

    • Onrefresh ()

Set Style tool Themesource

    • Registerlisteners ()
    • Finishbeanfactoryinitialization (Beanfactory)
    • Finishrefresh ()

More specific details can be found in: https://www.cnblogs.com/lkdirk/p/7145941.html .

SPRINGMVC Web. XML configuration SPRINGMVC IOC container initialization

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.