资讯专栏INFORMATION COLUMN

spring-framework源码研读

rottengeek / 2624人阅读

摘要:额外知识点参考知识点加载策略资源后,容器启动时会调用的方法。从获取对象对象,如果存在则抛异常。这个是重点核心的知识点,参考知识点。看到这里应该知道每一层的作用吧,一层一层往上递进第四步正在研读

1.根据我们常用的web.xml里,我们找到的org.springframework.web.context.ContextLoaderListener。web.xml如下



    moon
    
        org.springframework.web.context.ContextLoaderListener
     
    
        contextConfigLocation
        
            classpath*:applicationContext.xml
        
    

其实ContextLoaderListener是一个ServletContextListener,一个web容器的监听器。根据其父类org.springframework.web.context.ContextLoader初始化的策略配置,找到类目录下面的ContextLoader.properties文件。里面配置着一个Spring上下文类。

# Default WebApplicationContext implementation class for ContextLoader.
# Used as fallback when no explicit context implementation has been specified as context-param.
# Not meant to be customized by application developers.

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

额外知识点:
参考《ClassPathResource知识点》

加载策略资源(ContextLoader.properties)后,web容器启动时会调用ContextLoaderListener的contextInitialized方法。

contextInitialized方法里初始化ContextLoader.properties配置的org.springframework.web.context.support.XmlWebApplicationContext,这个初始化过程有点长。
4.1 从ServletContext获取对象springcontext对象,如果存在则抛异常。key=org.springframework.web.context.WebApplicationContext.ROOT
4.2 从ContextLoader.properties获取className=org.springframework.web.context.support.XmlWebApplicationContext
4.3 通过ClassUtils工具实例化spring的ApplicationContext,我们能够看出他是使用ContextLoader类的类加载器,这里有个可以弄个知识点《ClassUtils知识点》

ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader())

4.4 XmlWebApplicationContext实例化时,其一些列父类也将初始化和实例化(我稍微地简化说,具体的可以看源码):

层次 类名 作用
第0层 org.springframework.web.context.support.XmlWebApplicationContext 定义默认配置文件,如/WEB-INF/applicationContext.xml
第1层 org.springframework.web.context.support.AbstractRefreshableWebApplicationContext 声明了ServletContext servletContext和ServletConfig servletConfig
第2层 org.springframework.context.support.AbstractRefreshableConfigApplicationContext 此时就跟web没多大关系了,定义了配置变量String[] configLocations
第3层 org.springframework.context.support.AbstractRefreshableApplicationContext 此层就重要了,定义了DefaultListableBeanFactory beanFactory
第4层 org.springframework.context.support.AbstractApplicationContext 此层定义了父context,还有ApplicationContext的其他配置等,refresh()方法
第5层 org.springframework.core.io.DefaultResourceLoader 此层与ApplicationContext没多大关系,从包名也能看出,声明了ClassLoader
第6层(最后一层) org.springframework.core.io.ResourceLoader spring的作者的注释是Strategy interface for loading resources,嗯哈,大概猜是使用策略模式的接口

实例化完XmlWebApplicationContext类后,就开始做点事情了。在ContextLoader里,有个方法configureAndRefreshWebApplicationContext(cwac, servletContext),从方法名就能看出是配置和刷新ApplicationContext。
5.1 设置ServletContext,参考第1层
5.2 设置configLocations,参考第2层,这里又一个知识点《StringUtils知识点》
5.3 调用AbstractRefreshableWebApplicationContext的createEnvironment()方法来初始化StandardServletEnvironment对象。参考知识点《StandardServletEnvironment知识点》
5.4 调用StandardServletEnvironment对象initPropertySources(ServletContext servletContext, ServletConfig servletConfig)方法来获取web容器所有配置信息。配置信息都在放在StandardServletEnvironment对象的MutablePropertySources对象里。
5.5 customizeContext(sc, wac)自定义初始化applicationContext的一些信息,里面是获取实现org.springframework.context.ApplicationContextInitializer接口的一些方法调用。一个知识点也诞生《ApplicationContextInitializer知识点》

5.6 重头戏来了,refresh ApplicationContext!!!,调用第4层的refresh()方法。

  5.6.1 第一步:加载ServletContext和ServletConfig参数,并使用PropertySourcesPropertyResolver校验是否有必须的配置缺失,可参考知识点《PropertySourcesPropertyResolver知识点》。
  
  5.6.2 第二步:实例化BeanFactory,交给子类AbstractRefreshableApplicationContext来实例化(参考第3层),实例化了org.springframework.beans.factory.support.DefaultListableBeanFactory对象作为变量beanFactory的值。并配置是否允许覆盖bean和是否允许循环引用,默认都是否。
  
  
  5.6.3 第三步:第3层AbstractRefreshableApplicationContext去loadBeanDefinitions,loadBeanDefinitions方法被子类XmlWebApplicationContext覆写了,创建org.springframework.beans.factory.xml.XmlBeanDefinitionReader对象进行加载Bean的定义。这个是重点核心的知识点,参考《XmlBeanDefinitionReader知识点》。  
   
  看到这里应该知道每一层ApplicationContext的作用吧,一层一层往上递进
  
  5.6.4 第四步: //TODO 正在研读XmlBeanDefinitionReader

//TODO

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67936.html

相关文章

  • spring-framework的Resource知识点

    摘要:接口类三个具有代表性的实现类通过的和,我们找到利用去解析路径配置文件的路径。上面可能讲的有点绕,但却是入口之一。根据路径的特性,分别封装为或对象。另外用包里的做了实验,发现可以读到包里的信息。则是包的根地方,如,用于公共配置文件。 接口类:org.springframework.core.io.Resource 三个具有代表性的实现类: org.springframework.we...

    Jonathan Shieber 评论0 收藏0
  • 基于注解方式配置springMVC 并整合mybatis(一)

    摘要:在实战一书中前面两部分分别介绍了和的高级特性,并且基于类配置有一套层的,但是没有将层整合层,于是我试着整合了下,也方便以后写测试。 在《springBoot实战》 一书中前面两部分分别介绍了spring 和 springMVC的高级特性,并且基于java类配置有一套web层的demo,但是没有将web层整合dao层,于是我试着整合了下,也方便以后写测试demo。下面是我的整理 pom....

    岳光 评论0 收藏0
  • Electrum 钱包源码研读(一)

    摘要:一首先从网上下载代码导入公钥并验证源码签名,命令如下二对代码进行安装,命令如下三安装完毕之后,我们可以在终端中输入如下命令来查看帮助的信息如下运行运行守护程序使用一个未使用过的地址创建一个付款请求 一、首先从网上下载代码、导入gpg公钥并验证源码签名,命令如下: wget https://raw.githubusercontent... gpg --import ThomasV.a...

    Scorpion 评论0 收藏0
  • Electrum 钱包源码研读(一)

    摘要:一首先从网上下载代码导入公钥并验证源码签名,命令如下二对代码进行安装,命令如下三安装完毕之后,我们可以在终端中输入如下命令来查看帮助的信息如下运行运行守护程序使用一个未使用过的地址创建一个付款请求 一、首先从网上下载代码、导入gpg公钥并验证源码签名,命令如下: wget https://raw.githubusercontent... gpg --import ThomasV.a...

    or0fun 评论0 收藏0
  • Spring笔记03_AOP

    摘要:介绍什么是在软件业,为的缩写,意为面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。切面是切入点和通知引介的结合。切面类权限校验。。。 1. AOP 1.1 AOP介绍 1.1.1 什么是AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术...

    blair 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<