---
title: "看看Spring的contextLoader都做了什么"
author: "duyunfengsolo"
date: "2010-06-21T22:01:56Z"
tags: []
categories: ["企业架构"]
slug: "spring-contextloader"
draft: false
lastmod: "2010-06-21T22:01:56Z"
id: 695983
url: "https://solodu.iteye.com/blog/695983"
---

public class ContextLoader {

 

      

       public static final String CONTEXT\_CLASS\_PARAM = "contextClass";

 

//此处定义了spring配置文件的参数名称,和web.xml中的

&lt;context-param&gt;

     &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

     &lt;param-value&gt;/WEB-INF/applicationContext.xml,/WEB-INF/dataAccessContext-IBatis.xml&lt;/param-value&gt;

&lt;/context-param&gt;

完全对应,这个名字是不可更改的

       public static final String [CONFIG\_LOCATION\_PARAM]() = "contextConfigLocation";

 

      

       public static final String LOCATOR\_FACTORY\_SELECTOR\_PARAM = "locatorFactorySelector";

 

      

       public static final String LOCATOR\_FACTORY\_KEY\_PARAM = "parentContextKey";

 

      

       private static final String DEFAULT\_STRATEGIES\_PATH = "ContextLoader.properties";

 

 

       private static final Properties defaultStrategies;

 

       static {

              // Load default strategy implementations from properties file.

              // This is currently strictly internal and not meant to be customized

              // by application developers.

              try {

                     ClassPathResource resource = new ClassPathResource(DEFAULT\_STRATEGIES\_PATH, ContextLoader.class);

                     defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);

              }

              catch (IOException ex) {

                     throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());

              }

       }

 

 

       private final Log logger = LogFactory.getLog(ContextLoader.class);

 

       /\**

       * The root WebApplicationContext instance that this loader manages.

       \*/

       private WebApplicationContext context;

 

       /\**

       * Holds BeanFactoryReference when loading parent factory via

       * ContextSingletonBeanFactoryLocator.

       \*/

       private BeanFactoryReference parentContextRef;

 

 

      

       .......

      

       protected WebApplicationContext createWebApplicationContext(

                     ServletContext servletContext, ApplicationContext parent) throws BeansException {

 

              Class contextClass = determineContextClass(servletContext);

              if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {

                     throw new ApplicationContextException("Custom context class [" + contextClass.getName() +

                                   "] is not of type \[" + ConfigurableWebApplicationContext.class.getName() + "]");

              }

 

              [ConfigurableWebApplicationContext]() wac =

                            (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

              wac.setParent(parent);

              wac.setServletContext(servletContext);

//这里**getInitParameter()**方法概述：public java.lang.String getInitParameter(java.lang.String name)返回上下文定义的变量的值，如果变量不存在，返回null。见ServletConfig.getInitParameter (java.lang.String)。

              String [configLocation]() = [servletContext]().getInitParameter(CONFIG\_LOCATION\_PARAM);

//最后使用configLocation初始化wac对象

              if (configLocation != null) {

                     wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,

                                   ConfigurableWebApplicationContext.CONFIG\_LOCATION\_DELIMITERS));

              }

 

              wac.refresh();

              return wac;

       }

 

      

      .......

 

 

 

      

 

 

 

}