Basic concepts
The initialization process of the Spring IoC container is defined in the Listener Contextloaderlistener class.
Specifically implemented by the Configureandrefreshwebapplicationcontext method of the class, it contains two procedures:
- Configuration process
- Refresh Process
Principle Analysis
Here's a look at the specific implementation of the Configureandrefreshwebapplicationcontext method:
Represents the identity of the containerPublicStaticFinal String Context_id_param ="ContextID";Represents the configuration file path for a containerPublicStaticFinal String Config_location_param ="Contextconfiglocation";ProtectedvoidConfigureandrefreshwebapplicationcontext (Configurablewebapplicationcontext WAC, ServletContext SC) {if (Objectutils.identitytostring (WAC). Equals (Wac.getid ())) {Configuration process:1. Set the container's identity, i.e. ContextID String idparam = Sc.getinitparameter (Context_id_param);if (idparam! =NULL) {Wac.setid (idparam);}else {Wac.setid (Configurablewebapplicationcontext.application_context_id_prefix + objectutils.getdisplaystring ( Sc.getcontextpath ())); } }//2. Set the container's ServletContext Wac.setservletcontext (SC); //3. Set the container's profile path, which is contextconfiglocation String Configlocationparam = Sc.getinitparameter (Config_location_param) ; if (configlocationparam! = null) {wac.setconfiglocation (configlocationparam);} //4. Set the container's environment and initialize its properties configurableenvironment env = Wac.getenvironment (); //5. Initialize the container's ambient properties if (env instanceof configurablewebenvironment) {((configurablewebenvironment) env). Initpropertysources (SC, null);} //Custom process, temporarily do not explore Customizecontext (SC, WAC); //Refresh process: Wac.refresh ();}
The initialization process for Spring containers is actually broken down to two procedures: the configuration process, the refresh process
In the Contextloaderlistener class, the configuration process is mainly completed, that is, the Contextid,servletcontext,configlocation,configurableenvironment property of the container is set.
The refresh process is done by the Spring container you just created.
Configuration procedure 1. Setting the container's environment
When the Spring container is set to its Environment property, a Standardservletenvironment object is created by default if it does not exist. The specific inheritance relationship is as follows:
Take a look at the Getenvironment method:
private ConfigurableEnvironment environment;public ConfigurableEnvironment getEnvironment() { // 不存在,则创建 if (this.environment == null) { this.environment = createEnvironment(); } return this.environment;}protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment();}
Then analyze the initialization process of standardenvironment, which, during initialization, creates a Propertysources object to hold system-related environment variables and properties.
Abstractenvironment classPrivateFinal Mutablepropertysources propertysources =New Mutablepropertysources (This.logger);PublicAbstractenvironment () {customizepropertysources (This.propertysources);Omit part of the code ...}//standardenvironment class public Static final String system_environment_property_source_name = "Systemenvironment"; public static final String SYSTEM _properties_property_source_name = "systemproperties"; protected void customizepropertysources ( Mutablepropertysources propertysources) {propertysources.addlast (new MapPropertySource (System_properties_property_source_name, Getsystemproperties ())); Propertysources.addlast (new Systemenvironmentpropertysource (SYSTEM_ENVIRONMENT_ Property_source_name, Getsystemenvironment ()))}
2. Initializing the container's environment properties
That is, initializes the Propertysources property of the environment, which adds ServletContext, ServletConfig to the
The propertysources.
// StandardServletEnvironment 类public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) { // 将 servletContext、servletConfig 添加到 propertySources WebApplicationContextUtils.initServletPropertySources( getPropertySources(),servletContext, servletConfig);}public MutablePropertySources getPropertySources() { return this.propertySources;}
03.Spring IoC Container-initialization