03.Spring IoC 容器 - 初始化

來源:互聯網
上載者:User

標籤:csdn   pretty   表示   http   java   ==   getc   private   get   

基本概念

Spring IoC 容器的初始化過程在監聽器 ContextLoaderListener 類中定義。

具體由該類的的 configureAndRefreshWebApplicationContext 方法實現,它包含了兩個過程:

  • 配置過程
  • 重新整理過程
原理分析

下面來看 configureAndRefreshWebApplicationContext 方法的具體實現:

// 表示容器的標識public static final String CONTEXT_ID_PARAM = "contextId";// 表示容器的設定檔路徑public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";protected void configureAndRefreshWebApplicationContext( ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // 配置過程: // 1.設定容器的標識,即 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.設定容器的 ServletContext wac.setServletContext(sc); // 3.設定容器的設定檔路徑,即 ContextConfigLocation String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // 4.設定容器的環境,並初始化它的屬性 ConfigurableEnvironment env = wac.getEnvironment(); // 5.初始化容器的環境屬性 if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } // 自訂過程,暫不探究 customizeContext(sc, wac); // 重新整理過程: wac.refresh();}

Spring 容器的初始化過程實際被細分為了兩個過程:配置過程、重新整理過程

  • 在 ContextLoaderListener 類中主要完成了配置過程,即設定容器的 ContextId,ServletContext,ConfigLocation,ConfigurableEnvironment 屬性等。

  • 重新整理的過程則由剛剛建立 Spring 容器自己完成。

配置過程1.設定容器的環境

Spring 容器在設定的它的 Environment 屬性時,如果不存在則預設建立一個 StandardServletEnvironment對象。具體的繼承關係如下:

來看下 getEnvironment 方法:

private ConfigurableEnvironment environment;public ConfigurableEnvironment getEnvironment() {    // 不存在,則建立    if (this.environment == null) { this.environment = createEnvironment(); } return this.environment;}protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment();}

再來分析 StandardEnvironment 的初始化過程,該類在初始化過程中,會建立一個 propertySources 對象來儲存系統相關的環境變數與屬性。

// AbstractEnvironment 類private final MutablePropertySources propertySources =     new MutablePropertySources(this.logger);public AbstractEnvironment() { customizePropertySources(this.propertySources); // 省略部分代碼...}// StandardEnvironment 類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.初始化容器的環境屬性

即初始化 Environment 的 propertySources 屬性,它會將 ServletContext 、ServletConfig 添加到 
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 容器 - 初始化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.