这几天在看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的注解功能,完成请求和注解POJO的映射 --> 14. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 15. 16. <!-- 对模型视图名称的解析...
-- Spring 容器启动监听器 --> org.springframework.web.context.ContextLoaderListener </listener> <!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC...
<param-name>contextConfigLocation <param-value>/WEB-INF/spring/*.bean.xml org.springframework.web.context.ContextLoaderListener <listener> <listener-class>org.springframework....
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造 2.静态工厂 3.实例工厂 管理对象 对象关系DI 构造器注入 set注入 生命周期 scope:prototype/singleton init-...
SpringMVC配置控制器有两种方式,常用的是通过WEB-INF下的[servlet-name]-servlet.xml,本项目采用的是另外一种通过配置配置文件路径的方式来实现Springmvc的配置。
-- 设置Spring容器加载配置文件路径 --> <param-name>contextConfigLocation <param-value>classpath*:applicationContext-server.xml <listener-class>org.springframework.web.util....
springmvc的资源配置:Spring 刷新Introspector防止内存泄露,Spring MVC配置;Character Encoding filter; <param-name>contextConfigLocation <param-value>classpath*:applicationContext-*.xml ...
其实现类是apache的BasicDataSource,第11~25行定义了Hibernate的会话工厂,会话工厂类用Spring提供的LocalSessionFactoryBean维护,它注入了数据源和资源映射文件,此外还通过一些键值对设置了Hibernate所需的属性...
mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...
<param-name>contextConfigLocation /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener <filter-name>...
Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的默认值为WEB-INF/applicationContext.xml 。 我们已将此值定义为classpath:applicationContext.xml 。 这个文件代表了 Spring...
--servlet的参数配置,查找controller位置的xml文件配置,此参数指定了spring配置文件的位置 ,如果你不指定的话,默认会查找 */WEB-INF/<servlet name>-servlet.xml,其中由servlet的名字替换--> ...
* * * * Spring MVC的核心类和注解 请输入您的部门/机构名称 12.1 DispatcherServlet DispatcherServlet的全名是org.springframework.web.servlet.DispatcherServlet,它在程序中充当着前端控制器的角色。...
spring/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> ApplicationContext * * * *...
本教程是基于MyEclipse环境下基于maven搭建的springmvc步骤(图文教程),文章结尾含有源码下载地址: 新建maven project File->New->Project 选择Maven Project 选择项目路径 Use default Workspace location默认...
项目结构图(Spring3.0.2+mybatis3.0.4)应用数据库对应的实体类数据Dao接口在config目录下...节点配置contextConfigLocation属性是为了让ContextLoaderListener找到Spring上下文的位置并加载它,如果不指定contextConfig
web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者...
使用SSH构建项目的时候遇到了contextConfigLocation 和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-...
####1 Getting Started #####1.1 Introduction ... 首先在web.xml中配置,类似spring mvc,需要配置listener来启动spring context,配置servlet负责请求分发 <param>contextConfigLocation <param>class