spring开发小记:ApplicationContext 的创建

创建 ApplicationContext 有两种方式,最常用的一种方式就是以声明的方式创建,如使用 ContextLoader, 这也是绝大多数 web 应用采用的一种方式。另一种方式就是用 ApplicationContext 的借口实现类以编程的方式实现。
在这里我主要讲一下声明方式的实现及其原理:
首先讲一下 ContextLoader 接口,它有两个实现 :ContextLoaderListener ContextLoaderServlet. 其中常用的是 ContextLoaderListener. spring 文档上可以查到,他们二者实现的功能基本一样,只是 ContextLoaderListener 不能在与 Servlet2.2 兼容的 web 容器中使用。另外,因为 ContextLoaderLitener 是一个 servlet Listener ,因此,它是在 servlet context 建立后立即执行,也就以为这 servlet 已建立, spring ApplicationContext 就得到了初始化,并且能够相应第一个请求,所以首选 ContextLoaderListener.
下面是一个项目的 web.xml 文件的部分代码:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd"
>
 
< web-app >
    
< display-name > Baselib Application </ display-name >
    
    
< context-param >
        
< param-name > contextConfigLocation </ param-name >
        
< param-value >
        /WEB-INF/classes/spring-sup-middelbeans.xml
        /WEB-INF/classes/spring-ueaac-action.xml
        /WEB-INF/classes/spring-ueaac-beans.xml
        /WEB-INF/classes/spring-ueaac-cm.xml
        /WEB-INF/classes/spring-ueaac-hibernate.xml
        /WEB-INF/classes/spring-ueaac-resource.xml
        /WEB-INF/classes/spring-ueaac-sso.xml
        /WEB-INF/classes/com/javaeye/jert/application_context.xml
        
</ param-value >
    
</ context-param >
…………………………
< listener >
       
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
   
</ listener >
 
…………………
 
这里主要是配置了 spring 的监听器 ContextLoaderListener ,它检查 contextConfigLocation 这个参数。如果它不存在的话,它将用 /WEB-INF/applicationContext.xml 作为默认的配置文件。如果 contextConfigLocation 存在的话,它将根据该参数的值查找配置文件的位置,来一一读取 spring 参数。
  在这里要注意一个问题关于参数名称contextConfigLocation ,我个人认为是固定的,不能随意改动,因为我曾经做过试验,发现回出错,这时候监听器找不到contextConfigLocation参数,则用默认值,即它会去查找appilcationContext.xml文件,这时当然会抱错的拉。为了更有说服力,我查阅了一下spring源码,下面是ContextLoader.java的部分源码:
 
 
package  org.springframework.web.context;
publicclass ContextLoader 
{
 
    publicstaticfinal String CONTEXT_CLASS_PARAM 
= "contextClass";
 
    
/**
     *Nameofservletcontextparameterthatcanspecifytheconfiglocation
     *fortherootcontext,fallingbacktotheimplementation'sdefault
     *otherwise.
     *
     
*/

    publicstaticfinal String CONFIG_LOCATION_PARAM 
= "contextConfigLocation";
 
    publicstaticfinal String LOCATOR_FACTORY_SELECTOR_PARAM 
= "locatorFactorySelector";
 
    
    publicstaticfinal String LOCATOR_FACTORY_KEY_PARAM 
= "parentContextKey";
 
    
    privatestaticfinal String DEFAULT_STRATEGIES_PATH 
= "ContextLoader.properties";
 
 
    privatestaticfinal 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) {
           thrownew IllegalStateException(
"Could not load 'ContextLoader.properties': " + ex.getMessage());
       }

    }

 
…………………………………………………
      
其中有一个属性
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation" ;
说明了 contextConfigLocation 是被 spring 固定的,专门用来查  找配置文件位置的
 

你可能感兴趣的:(spring开发小记:ApplicationContext 的创建)