spring开发小记:ApplicationContext 的创建

本文介绍 Spring 应用上下文 ApplicationContext 的两种创建方式,重点讲解了通过 ContextLoaderListener 进行声明式配置的方法,包括其工作原理及 web.xml 中的具体配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
创建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固定的,专门用来查 找配置文件位置的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值