这几天在看spring的源码,涉及到spring启动位置的部分,下面就看看spring到底是从哪儿开始加载的。本文使用的是spring3.0M3
首先spring的加载会借助一个监听器ContextLoaderListener,直接上web.xml文件
Xml代码
1.<listener>
2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3.</listener>
我们通常会对加载位置统一管理
Xml代码
1.<context-param>
2. <param-name>contextConfigLocation</param-name>
3. <param-value>
4. /WEB-INF/conf/spring/**/*.xml
5. </param-value>
6. </context-param>
这个org.springframework.web.context.ContextLoaderListener类型是springframework中的原始加载上下文的监听器,
通常我们会自定义一个Listener去继承ContextLoaderListener并另外实现我们需要初始化的接口(通常我们会选择实现一些接口来对session的管理)
Java代码
1.public class FrameServletContextListener extends ContextLoaderListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
2. //
3. private ServletContext initPath(ServletContextEvent event) {
4.
5. }
6.
7. public synchronized void contextDestroyed(ServletContextEvent event) {
8. //
9. }
10.
11. ...
12.}
当监听器设置好了之后 ,启动web容器 监听器开始启动ContextLoaderListenerl
类中的方法contextInitialized()
Java代码
1./**
2. * Initialize the root web application context.
3. */
4.public void contextInitialized(ServletContextEvent event) {
5. this.contextLoader = createContextLoader();
6. if (this.contextLoader == null) {
7. this.contextLoader = this;
8. }
9. this.contextLoader.initWebApplicationContext(event.getServletContext());
10.}
这样this.contextLoader.initWebApplicationContext(event.getServletContext());ContextLoaderListener
就会借助容器的上下文去初始一个spring的应用上下文,使用到了ContextLoader这个类
在ContextLoader初始化时我们看到这样一块static代码
Java代码
1.static {
2. // Load default strategy implementations from properties file.
3. // This is currently strictly internal and not meant to be customized
4. // by application developers.
5. try {
6. //这一句会去加载同在此包下的一个properties文件的值(ContextLoader.properties)
7. ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
8. defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
9. }
10. catch (IOException ex) {
11. throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
12. }
13.}
属性文件中这样定义
引用
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
这样我们就能根据属性文件中的定义反射出一个XmlWebApplicationContext上下文了
然而我们在XmlWebApplicationContext中看到如下变量
Java代码
1./** Default config location for the root context */
2.public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
至此我们已经知道默认加载spring文件的启动位置了
当我们再看ContextLoader类,我们就会看到传说中的参数contextConfigLocation
Java代码
1.public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
而XmlWebApplicationContext对象正是调用了这个参数去设置启动位置
Java代码
1.wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
再往上看XmlWebApplicationContext继承的AbstractRefreshableConfigApplicationContext类中的setConfigLocation方法将此抽象类中的String[] configLocations值填充
并在AbstractRefreshableConfigApplicationContext类中我们看到spring对默认启动文件位置和配置启动文件位置的支持
Java代码
1.protected String[] getConfigLocations() {
2. return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}
至此我们已经清楚spring将从哪儿加载并知道加载哪些文件了。
分享到:
相关推荐
SpringSecurity加载流程图 SpringSecurity系统启动流程 SpringSecurity调用流程
Spring IoC加载流程讲解以及IoC思想和依赖倒置原则
博文链接:https://eastpoint.iteye.com/blog/78983
springioc加载流程图.pdf
本篇将对定义在 XMl 文件中的 bean,从静态的的定义到变成可以使用的对象的过程,即 bean 的加载和获取的过程进行一个整体的了解
整理了一下spring的加载, 个人对于Spring的流程的理解,望多多指正
springBean加载过程源码解析文档,附有代码类名和行数
spring加载restful(文档+程序源码) 通过REST风格体系架构,请求和响应都是基于资源表示的传输来构建的。资源是通过全局ID来标识的,这些ID一般使用的是一个统一资源标识符(URI)。客户端应用使用HTTP方法(如,...
spring启动componentscan类扫描加载过程—源码分析Java开发Java经验技巧共16页.pdf.zip
spring源码解析,详细版思维导图,主要讲解了加载的整个过程
Spring的IOC容器—BeanFactory—容器加载过程解析容器加载DefaultListableBeanFactory类介绍BeanFactory接口介绍BeanDefinition接口介绍BeanDefinitionRegistry接口介绍如何解析xml文件 容器加载 ...
详细的描述了testNG集成spring的配置过程,以及示例。
SPI 机制(Java SPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制), 很多地方有用到: AOP Spring的AOP的底层实现原理; 为什么jdk动态代理是必须是接口 两种动态代理的区别 AOP实现方式:...
14. Nacos加载那些配置,这些配置的优先级? 15. Nacos配置中⼼宕机了,会影响服务吗? 16. 配置中⼼的技术选型 17. 为什么Feign第⼀次调⽤耗时很⻓? 18. Feign怎样实现认证的传递? 19. Feign底层默认使⽤...
主要给大家介绍了关于在Spring Boot中加载XML配置的完整步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
应用程序启动:应用程序启动时,Spring框架会加载并初始化所需的配置文件和类。 配置加载:Spring框架会读取并加载应用程序的配置文件,例如XML配置文件或基于注解的配置类。 依赖注入:Spring框架使用依赖注入...
本文深入探讨了Spring IoC容器的加载过程及其源码实现,揭示了Spring中最为根本的概念之一。这包括从AnnotationConfigApplicationContext的实例化开始,到DefaultListableBeanFactory工厂的建立,再到...
JPA快速使用---我瞎写了》从核心实现和企业应用两个方面,由浅入深、由易到难地对Spring源码展开了系统的讲解,包括Spring的设计理念和整体架构、容器的基本实现、默认标签的解析、自定义标签的解析、bean的加载、...
从加载过程我们可以看到,首先从Servlet事件中得到ServletContext,然后可以读到配置好的在web.xml的中的各个属性值,然后ContextLoder实例化WebApplicationContext并完成其载入和初始化作为根上下文。当这个根上...
3.10 Spring中的AspectJ加载时织入aspect 140 3.10.1 问题 140 3.10.2 解决方案 141 3.10.3 工作原理 141 3.11 在Spring中配置AspectJ aspect 146 3.11.1 问题 146 3.11.2 解决方案 146 3.11.3 工作...