Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > spring开发小记:ApplicationContext 的创建
【标  题】:spring开发小记:ApplicationContext 的创建
【关键字】:spring,ApplicationContext
【来  源】:http://blog.csdn.net/jimmy_developing/archive/2006/10/24/1349568.aspx

spring开发小记:ApplicationContext 的创建

Your Ad Here  
创建ApplicationContext有两种方式,最常用的一种方式就是以声明的方式创建,如使用ContextLoader,这也是绝大多数web应用采用的一种方式。另一种方式就是用ApplicationContext的借口实现类以编程的方式实现。
在这里我主要讲一下声明方式的实现及其原理:
首先讲一下ContextLoader接口,它有两个实现:ContextLoaderListener ContextLoaderServlet.其中常用的是ContextLoaderListener.spring 文档上可以查到,他们二者实现的功能基本一样,只是ContextLoaderListener不能在与Servlet2.2兼容的web容器中使用。另外,因为ContextLoaderLitener是一个servlet Listener,因此,它是在servlet context建立后立即执行,也就以为这servlet已建立,springApplicationContext就得到了初始化,并且能够相应第一个请求,所以首选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());
       }

    }

 
…………………………………………………
      
其中有一个属性
publicstaticfinal String CONFIG_LOCATION_PARAM = "contextConfigLocation";
说明了contextConfigLocation是被spring固定的,专门用来查 找配置文件位置的
再来一个java小程序:【上一篇】
很小很怪的一个java程序:【下一篇】
【相关文章】
  • SpringSide 2.0 社区全新组团盛大开放
  • 幼学琼林--Spring下的单元测试要点
  • 采用struts+hibernate+spring开发的一个web系统(7)--系列截至
  • dwr与spring相结合
  • 在Spring中使用replaced-method来进行方法替换
  • SpringXmlrpcServiceExporter for xmlrpc3.x
  • JSF-Spring的新特性
  • Yale CAS as an Acegi Client in SpringSide
  • struts+hibernate+spring开发的一个web系统,正式提供源码下载
  • spring的中文reference
  • 【随机文章】
  • JTextArea用法
  • 接口的用法
  • 多元化的DSL技术与方案
  • http://www.cublog.cn/u/10957/?u=http://www.cublog.
  • 一个使用setjmp/longjmp从信号中恢复的小例子
  • 类基本分析.doc
  • UML 简单描述
  • @_@让eva在设定的时间自动启动并登录(这下,帐号密码可以随意输入啦)
  • Shadow Mapping Using OpenGL
  • 一个快速开关 Web Service进程 的 .bat 文件 by Stabx
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.