`
yuchengyang
  • 浏览: 10389 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring默认启动位置以及contextConfigLocation设置源码解析

阅读更多
这几天在看spring的源码,涉及到spring启动位置的部分,下面就看看spring到底是从哪儿开始加载的。本文使用的是spring3.0M3

首先spring的加载会借助一个监听器ContextLoaderListener,直接上web.xml文件
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



我们通常会对加载位置统一管理  
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
			/WEB-INF/conf/spring/**/*.xml
        </param-value>
    </context-param>

这个org.springframework.web.context.ContextLoaderListener类型是springframework中的原始加载上下文的监听器,
通常我们会自定义一个Listener去继承ContextLoaderListener并另外实现我们需要初始化的接口(通常我们会选择实现一些接口来对session的管理)

public class FrameServletContextListener extends ContextLoaderListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
	//
	private ServletContext initPath(ServletContextEvent event) {

	}

    	public synchronized void contextDestroyed(ServletContextEvent event) {
	//
	}

	...
}

当监听器设置好了之后 ,启动web容器 监听器开始启动ContextLoaderListenerl
类中的方法contextInitialized()
	/**
	 * Initialize the root web application context.
	 */
	public void contextInitialized(ServletContextEvent event) {
		this.contextLoader = createContextLoader();
		if (this.contextLoader == null) {
			this.contextLoader = this;
		}
		this.contextLoader.initWebApplicationContext(event.getServletContext());
	}

这样this.contextLoader.initWebApplicationContext(event.getServletContext());ContextLoaderListener
就会借助容器的上下文去初始一个spring的应用上下文,使用到了ContextLoader这个类



在ContextLoader初始化时我们看到这样一块static代码
	static {
		// Load default strategy implementations from properties file.
		// This is currently strictly internal and not meant to be customized
		// by application developers.
		try {
			//这一句会去加载同在此包下的一个properties文件的值(ContextLoader.properties)
			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());
		}
	}

属性文件中这样定义
引用
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

这样我们就能根据属性文件中的定义反射出一个XmlWebApplicationContext上下文了

然而我们在XmlWebApplicationContext中看到如下变量

	/** Default config location for the root context */
	public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

至此我们已经知道默认加载spring文件的启动位置了


当我们再看ContextLoader类,我们就会看到传说中的参数contextConfigLocation
	public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";


而XmlWebApplicationContext对象正是调用了这个参数去设置启动位置
		wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));


再往上看XmlWebApplicationContext继承的AbstractRefreshableConfigApplicationContext类中的setConfigLocation方法将此抽象类中的String[] configLocations值填充

并在AbstractRefreshableConfigApplicationContext类中我们看到spring对默认启动文件位置和配置启动文件位置的支持
	protected String[] getConfigLocations() {
		return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}
至此我们已经清楚spring将从哪儿加载并知道加载哪些文件了。
分享到:
评论

相关推荐

    spring_MVC源码

    -- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; 14. &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; 15. 16. &lt;!-- 对模型视图名称的解析...

    springweb3.0MVC注解(附实例)

    -- Spring 容器启动监听器 --&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC...

    spring和hibernate整合

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/WEB-INF/spring/*.bean.xml org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework....

    Spring MVC 入门实例

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...

    Spring.html

    Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造 2.静态工厂 3.实例工厂 管理对象 对象关系DI 构造器注入 set注入 生命周期 scope:prototype/singleton init-...

    Springmvc-annotation_contextConfigLocation

    SpringMVC配置控制器有两种方式,常用的是通过WEB-INF下的[servlet-name]-servlet.xml,本项目采用的是另外一种通过配置配置文件路径的方式来实现Springmvc的配置。

    CXF WebService整合Spring示例工程代码demo

    -- 设置Spring容器加载配置文件路径 --&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-server.xml &lt;listener-class&gt;org.springframework.web.util....

    Spring的web配置

    springmvc的资源配置:Spring 刷新Introspector防止内存泄露,Spring MVC配置;Character Encoding filter; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-*.xml ...

    ssh(structs,spring,hibernate)框架中的上传下载

    其实现类是apache的BasicDataSource,第11~25行定义了Hibernate的会话工厂,会话工厂类用Spring提供的LocalSessionFactoryBean维护,它注入了数据源和资源映射文件,此外还通过一些键值对设置了Hibernate所需的属性...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    Spring MVC 框架应用实例

    &lt;param-name&gt;contextConfigLocation /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的默认值为WEB-INF/applicationContext.xml 。 我们已将此值定义为classpath:applicationContext.xml 。 这个文件代表了 Spring...

    Spring MVC Demo

    --servlet的参数配置,查找controller位置的xml文件配置,此参数指定了spring配置文件的位置 ,如果你不指定的话,默认会查找 */WEB-INF/&lt;servlet name&gt;-servlet.xml,其中由servlet的名字替换--&gt; ...

    基于java的企业级应用开发:Spring MVC的核心类和注解.ppt

    * * * * Spring MVC的核心类和注解 请输入您的部门/机构名称 12.1 DispatcherServlet DispatcherServlet的全名是org.springframework.web.servlet.DispatcherServlet,它在程序中充当着前端控制器的角色。...

    基于java的企业级应用开发:Spring的核心容器.ppt

    spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; ApplicationContext * * * *...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    本教程是基于MyEclipse环境下基于maven搭建的springmvc步骤(图文教程),文章结尾含有源码下载地址: 新建maven project File-&gt;New-&gt;Project 选择Maven Project 选择项目路径 Use default Workspace location默认...

    Spring+mybatis整合实例应用

    项目结构图(Spring3.0.2+mybatis3.0.4)应用数据库对应的实体类数据Dao接口在config目录下...节点配置contextConfigLocation属性是为了让ContextLoaderListener找到Spring上下文的位置并加载它,如果不指定contextConfig

    web.xml中如何设置配置文件的加载路径实例详解

    web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者...

    MyContextLoaderPlugIn.jar

    使用SSH构建项目的时候遇到了contextConfigLocation 和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-...

    rural:基于spring的mvc框架,使用简单,URL映射零配置零注解

    ####1 Getting Started #####1.1 Introduction ... 首先在web.xml中配置,类似spring mvc,需要配置listener来启动spring context,配置servlet负责请求分发 &lt;param&gt;contextConfigLocation &lt;param&gt;class

Global site tag (gtag.js) - Google Analytics