`
zhang_ly520
  • 浏览: 158959 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring的加载过程(web) (1)

阅读更多

自己看源码总结的不一定准确,记录下来以备以后查看。

1.通过web.xml配置ContextLoaderListener 或者 ContextLoaderServlet 来加载spring.

2.ContextLoaderListener 或者 ContextLoaderServlet 两个类都是调用createContextLoader()方法

  new一个ContextLoader 实例。

3.ContextLoader 通过一个static方法:

 

static { try { 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()); } }

 

 

加载默认的策略,spring不建议用户自己定义策略,策略定义在与ContextLoader同一目录下的ContextLoader.properties。注释(Load default strategy implementations from properties file.This is currently strictly internal and not meant to be customized by application developers.) 

 

4.调用ContextLoader的initWebApplicationContext方法。

1)首先判断ServletContext中是否包含WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值这个属性。

2)加载ServletContext的父context,我测试的时候返回的是null.

3)自调用createWebApplicationContext创建根WebApplicationContext。

  <1>首先调用determineContextClass判断使用的是默认的XmlWebApplicationContext或者自定义的一个类。

  <2>实例化contextClass,并强制转化为ConfigurableWebApplicationContext接口

  <3>设置父context,设置servletContext,设置配置文件的位置。

  <4>调用refresh(ConfigurableApplicationContext接口中定义,AbstractApplicationContext类中实现)。

4)把context设置到 ServletContext中。属性名:WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
初始化完毕,其中红色部分进行了配置文件的加载.研究中。。。。

//===================================补充说明红色标记部分====================================

#加载配置文件
1.prepareRefresh 准备context进行refresh。准备refresh context,设置他的启动时间和使用状态.

2.obtainFreshBeanFactory 得到刷新后的BeanFactory.

 1)首先会调用refreshBeanFactory(真正的加载方法),由子类提供。

   AbstractApplicationContext有两个直接子类一个AbstractRefreshableApplicationContext,一个GenericApplicationContext 前者AbstractRefreshableApplicationContext.refreshBeanFactory()(此方法不可以重载)初始化BeanFacotry,后者不支持多次刷新是通过DefaultListableBeanFactory.registerBeanDefinition()注册。
 2)调用getBeanFactory()返回ConfigurableListableBeanFactory类型的BeanFactory

 3)refreshBeanFactory方法暂时没研究

 

3.bean的生命周期开始.

    1)prepareBeanFactory(beanFactory);在context中准备BeanFactory使用

    2)postProcessBeanFactory(beanFactory);登记BeanPostProcessor
    3)invokeBeanFactoryPostProcessors(beanFactory);调用作为bean在context注册的factory processors
    4)registerBeanPostProcessors(beanFactory);注册拦截bean创建的BeanProcessors
    5)initMessageSource();为本context初始化消息源
    6)initApplicationEventMulticaster(); 为context初始化事件的多点传送
    7)onRefresh(); 默认是空实现

    8)registerListeners();检测监听器bean并且注册他们

    9)beanFactory.preInstantiateSingletons();实例化单例允许他们存储信息资源
  10)publishEvent(new ContextRefreshedEvent(this));

       最后一步:公布对应的事件.实例化一个ContextRefreshedEvent()事件.

分享到:
评论

相关推荐

    加载spring 文件,在web.xml中的配置

    加载spring 文件,在web.xml中的配置

    springweb3.0MVC注解(附实例)

    -- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--&gt; &lt;servlet-name&gt;annomvc org.springframework.web.servlet.DispatcherServlet &lt;load-on-startup&gt;2 ...

    spring jar 包详解

    9) spring -hibernate.jar需spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-jdbc.jar,spring-orm.jar,spring-web.jar,spring-webmvc.jar 10) spring-orm.jar需spring-core.jar,...

    spring加载restful(文档+程序源码)

    spring加载restful(文档+程序源码) 通过REST风格体系架构,请求和响应都是基于资源表示的传输来构建的。资源是通过全局ID来标识的,这些ID一般使用的是一个统一资源标识符(URI)。客户端应用使用HTTP方法(如,...

    Spring-asm类加载修改/Tomcat Spring web项目源代码加密

    为了防止产品代码泄漏或授权等被破解,想到对源码加密,说是对源码加密,...本文讲解的就是SpringWeb项目加密后如何能在tomcat下面启动运行的。 1、war包加密; 2、tomcat类加载器修改; 3、Spring-asm类加载修改;

    项目配置文件( spring-mvc.xml spring-mybatis.xml web.xml log4j.properties)

    spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    java servlet spring web filter listenr

    testNG集成spring

    详细的描述了testNG集成spring的配置过程,以及示例。

    Spring加载XSD文件发生错误的解决方法

    主要介绍了Spring加载XSD文件发生错误的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring MVC 配置文档

    加载 DispatcherServlet 时 Spring 会尝试读取配置文件 默认的配置文件位于 web.xml 相同的路径下 文件名与注册的 Servlet 名有关 Servlet注册名跟上 -servlet.xml 例如:上面的 Servlet 注册名为 ...

    让spring加载自己的properties配置文件,在代码中获得配置信息

    在web应用用写的一些Socket服务等,需要配置IP、端口等信息,我们希望这些信息写到配置文件中,并有spring帮我们加载。

    Spring Web项目spring配置文件随服务器启动时自动加载

    主要介绍了Spring Web项目spring配置文件随服务器启动时自动加载,加载spring的配置文件,并且只加载一次,从而提高程序效率。具体内容详情大家通过本文一起学习吧

    spring源代码解析

    在web容器中启动Spring应用程序就是一个建立这个上下文体系的过程。Spring为web应用提供了上下文的扩展接口 WebApplicationContext: 如转载请注明,转载自:关注Java[http://www.gbsou.com] 本文链接: ...

    Spring攻略(第二版 中文高清版).part1

    第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...

    logback-ext-spring

    spring使用logback的扩展,使用起来非常方便。在web.xml中配置: ... &lt;listener-class&gt;ch.qos.logback.ext.spring.web.LogbackConfigListener &lt;/listener&gt; 即可加载logback配置,使用logback。

    Spring.html

    织入weaving:讲切面加载进内存形成代理对象的过程 代理Proxy 底层实现 JDK动态代理(默认) 基于接口:代理对象与目标对象是兄弟关系,目标类必须实现接口 CGLIB动态代理 基于父类:代理对象与目标对象是...

    Spring攻略(第二版 中文高清版).part2

    第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring入门.docx

    一、Spring基础 1.Xml配置Bean (1)DI注入属性:name为set方法去掉set之后首字母小写,ref为bean的id,需要有set方法...不同环境下会出现不同的容器,比如导入spring-web包时,会有web环境下的容器。 (4)基于xml

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

Global site tag (gtag.js) - Google Analytics