美文网首页
spring framework 源码分析,从入口开始

spring framework 源码分析,从入口开始

作者: yanjf90 | 来源:发表于2017-03-15 16:31 被阅读0次

本文结合上一篇编译spring的源码和单元测试以及spring官方文档,从细节入手,学习spring frmaework的使用方法以及实现原理,另一个目的:学会如何查看开源项目源码


首先从spring入口开始,spring编译好的源码中找到/spring-webmvc/src/test/resources/org/springframework/web/context/WEB-INF/web.xml文件,为什么先找这个文件?(Web.xml详解 )

<!-- This servlet must be loaded first to configure the log4j system and create the WebApplicationContext -->
<servlet>
    <servlet-name>config</servlet-name>
    <servlet-class>org.springframework.framework.web.context.ContextLoaderServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.framework.web.context.XMLWebApplicationContext</param-value>       
    </init-param>
    <init-param>
        <param-name>log4jPropertiesUrl</param-name>
        <param-value>/WEB-INF/log4j_PRODUCTION.properties</param-value>     
    </init-param>
    <!-- This is essential -->
    <load-on-startup>1</load-on-startup>
  </servlet>

可以看到首先加载org.springframework.framework.web.context.ContextLoaderServlet文件,再eclipse 中Ctrl+Shift+R,搜索此文件,竟然没找到😢,出师不利呀.

百度“找不到ContextLoaderServlet”,提示因在Spring3.0中去掉了ContextLoaderServletLog4jConfigServlet,所以会出现找不到类的异常错误。Spring3.0下可以采用另外两种启动方式:ContextLoaderListenerContextLoaderPlugIn。建议使用ContextLoaderListener。于是查找ContextLoaderListener文件,这个总算是有,不明白为什么源码和配置文件不匹配呢?然后网上找了一个使用ContextLoaderListener的配置web.xml

<!-- 项目中使用Spring 时,applicationContext.xml配置文件中并没有BeanFactory,要想在业务层中的class 文件中直接引用Spring容器管理的bean可通过以下方式-->  
<!--1、在web.xml配置监听器ContextLoaderListener-->  
<!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。  
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。  
它的API说明  
第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。  
如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口  
第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->  
BeanFactory这样一来spring中的所有bean都由这个类来创建  
 IUploaddatafileManager uploadmanager = (IUploaddatafileManager)    ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");
 -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<!--2、部署applicationContext的xml文件-->  
<!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,  
在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。  
如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:  
在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。  
也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,  
applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。  
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。-->  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:spring/applicationContext.xml</param-value>  
</context-param>  

1.如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml, 在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml
2.如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数
带着问题看源码,1. 默认路径如何指定?2. 自定义文件名如何实现的?
打开ContextLoaderListener.java 文件。

  /**
    * Initialize the root web application context.
    */
   @Override
   public void contextInitialized(ServletContextEvent event) {
       initWebApplicationContext(event.getServletContext());
   }```
进入`initWebApplicationContext `方法
![initWebApplicationContext
](https://img.haomeiwen.com/i2783430/03bac226391c8edd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
此方法中设置自定义配置文件,如果没指定,通过refresh()方法调用默认路径
1. 默认路径如何指定?
![determineContextClass](https://img.haomeiwen.com/i2783430/40b26f2ecf3f98fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
未指定contextClassName时,从defaultStrategies取默认className
![defaultStrategies](https://img.haomeiwen.com/i2783430/c6a76bdf1aa6ba23.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![ContexLoad.properties](https://img.haomeiwen.com/i2783430/5d127fa0716c8980.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
wac 返回默认的XmlWebApplicationContext实例。
![configureAndRefreshWebApplicationContext](https://img.haomeiwen.com/i2783430/6f6ea554f3cffd60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
查找`XmlWebApplicationContext`的`refresh()`方法,该类中未找到,一直往上层父类中寻找,最终`AbstractApplicationContext`中找到。
![refresh_getDefaultConfigLocations](https://img.haomeiwen.com/i2783430/5121dfe1a51ac594.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
`refresh()` 一级级调用直到`XmlWebApplicationContext`的`getDefaultConfigLoactions()`方法
![getDefaultConfigLocations](https://img.haomeiwen.com/i2783430/e02c18babd6f24dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
返回默认的配置文件地址
2. 自定义文件名如何实现的?
`public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";`
![configureAndRefreshWebApplicationContext](https://img.haomeiwen.com/i2783430/6f6ea554f3cffd60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
检查contextConfigLocation参数是否指定?如果有,wac.setConfigLocation()添加自定义配置文件。
![getConfigLocations](https://img.haomeiwen.com/i2783430/2d7f0d8462d02e4f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
如果已经指定自定义文件了,就不再使用默认位置。之前跟到这个地方发现`getDefaultConfigLocation()` 返回`null`,并未返回defaultContextLoaction。后来想明白是因为wac 是`XmlWebApplicationContext`实例,也应该调用`XmlWebApplicationContext`的`getDefaultConfigLocation()`方法,而不是此处父类方法。

相关文章

网友评论

      本文标题:spring framework 源码分析,从入口开始

      本文链接:https://www.haomeiwen.com/subject/enhlnttx.html