Spring源码阅读环境搭建

本文将粗略的搭建一个Spring源码的阅读环境,为后面的源码阅读做一个准备。做任何事情不管是有一个完美的或者是不太完美的开头,只要去做了,那么就是一种胜利。
由于spring使用了gradle构建工具,接下来先安装gradle。

安装gradle#

  • 从Gradle官网下载gradle安装包,打开https://gradle.org/releases/
    Spring源码阅读环境搭建
  • 将下载的安装包gradle-x.x.x-all.zip解压到当前目录
    Spring源码阅读环境搭建
  • 环境变量配置
    • 配置GRADLE_HOME
      Spring源码阅读环境搭建
    • 配置Path
      Spring源码阅读环境搭建
  • 打开目录行工具,输入gradle -v,能看到gradle的版本信息表示安装已经成功
    Spring源码阅读环境搭建

导入Spring源码#

Spring在github上的仓库地址是:https://github.com/spring-projects/spring-framework,本文不会直接去github上去下载源码,网速实在太慢。本文使用的码云上Spring仓库的镜像,该镜像每日同步一次,地址是https://gitee.com/mirrors/Spring-Framework

  • 从git导入项目
    Spring源码阅读环境搭建

  • 填写要克隆的git仓库信息,可以点击右边的【Test】按钮测试,等待仓库克隆完成
    Spring源码阅读环境搭建

  • 打开导入的Spring项目
    Spring源码阅读环境搭建

  • 从gradle导入Spring项目,等待gradle build完成
    Spring源码阅读环境搭建
    注意:

    • 上面使用的是本地自己安装的gradle。
    • idea中gradle默认的服务目录路径是用户目录下的.gradle目录,对于Administrator用户,对应的目录是C:\Users\Administrator\.gradle。该目录占用的空间一般比较多,所以在这里将这个目录放到其他的盘中。
  • 构建完成后报错如下(只列出了一部分):

 

Copy

... Error:(63, 30) java: cannot find symbol symbol: class Signature location: class org.springframework.cglib.core.KeyFactory ... location: class org.springframework.cglib.proxy.Enhancer Error:(152, 30) java: cannot find symbol ...

spring未了避免与cglib和objenesis冲突,将cglib和objenesis相关的包重新repack到org.springframework.cgliborg.springframework.objenesis包中,这部分的代码没有包含到源码当中。构建之前,可以通过添加Gradle任务来解决,见:https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md#known-issueshttps://youtrack.jetbrains.com/issue/IDEA-160605
解决办法如下:

  • 在idea中打开Gradle面板
    Spring源码阅读环境搭建
  • 在右侧的Gradle面板Spring -> Tasks -> other -> cglibRepackJar
    Spring源码阅读环境搭建
  • **任务
    Spring源码阅读环境搭建
  • 选择要**的cglibRepackJar任务
    Spring源码阅读环境搭建
  • 重新构建项目(花费的时间较长)
    Spring源码阅读环境搭建

创建测试模块my-test#

为了方便编写测试spring的代码,在spring-framework单独新建一个模块my-test

  • 右键spring-framework项目->New->Module...
    Spring源码阅读环境搭建

  • 输入ArtifactId: my-test
    Spring源码阅读环境搭建

  • 一路下一步,最后点击完成,新的模块就建好了

  • 添加依赖:api(project(":spring-context"))
    Spring源码阅读环境搭建

  • 为了能让my-test自动导入相关的依赖,在Gradle面板中右键spring节点
    Spring源码阅读环境搭建

  • 在my-test模块中编写程序测试

    • 创建MyApplication

       

      Copy

      package com.zfx; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Hello hello = (Hello)ac.getBean("hello"); hello.sayHello(); } }
    • 在resources目录下新建applicationContext.xml

       

      Copy

      <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.zfx.Hello"></bean> </beans>
    • 新建Hello

       

      Copy

      package com.zfx; public class Hello { public void sayHello() { System.out.println("Hello, zhangfengxian"); } }
    • 运行MyApplication,可以看到控制台输出:Hello, zhangfengxian

  • 至此整个环境算是搭建好了

其他问题#

spring-aspects模块构建时报错#

解决办法一:排除spring-aspects模块
在工具栏点击File -> Project Structure...
Spring源码阅读环境搭建
解决办法二:使用Acj或者Eclipse编译
在工具栏点击File -> Settings...
Spring源码阅读环境搭建
此问题的相关链接:

本文思维导图#

Spring源码阅读环境搭建

 

原文地址:https://www.cnblogs.com/zhangfengxian/p/11072500.html