本文将粗略的搭建一个Spring源码的阅读环境,为后面的源码阅读做一个准备。做任何事情不管是有一个完美的或者是不太完美的开头,只要去做了,那么就是一种胜利。

由于spring使用了gradle构建工具,接下来先安装gradle。

安装gradle

  • 从Gradle官网下载gradle安装包,打开https://gradle.org/releases/

  • 将下载的安装包gradle-x.x.x-all.zip解压到当前目录

  • 环境变量配置
    • 配置GRADLE_HOME

    • 配置Path

  • 打开目录行工具,输入gradle -v,能看到gradle的版本信息表示安装已经成功

导入Spring源码

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

  • 从git导入项目

  • 填写要克隆的git仓库信息,可以点击右边的【Test】按钮测试,等待仓库克隆完成

  • 打开导入的Spring项目

  • 从gradle导入Spring项目,等待gradle build完成



    注意:

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

...
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面板

  • 在右侧的Gradle面板Spring -> Tasks -> other -> cglibRepackJar

  • 激活任务

  • 选择要激活的cglibRepackJar任务

  • 重新构建项目(花费的时间较长)

创建测试模块my-test

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

  • 右键spring-framework项目->New->Module...

  • 输入ArtifactId: my-test

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

  • 添加依赖:api(project(":spring-context"))

  • 为了能让my-test自动导入相关的依赖,在Gradle面板中右键spring节点

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

    • 创建MyApplication

      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

      <?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

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

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

其他问题

spring-aspects模块构建时报错

解决办法一:排除spring-aspects模块

在工具栏点击File -> Project Structure...



解决办法二:使用Acj或者Eclipse编译

在工具栏点击File -> Settings...



此问题的相关链接:

本文思维导图

Spring源码阅读环境搭建的更多相关文章

  1. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  2. 搭建 Spring 源码阅读环境

    前言 有一个Spring源码阅读环境是学习Spring的基础.笔者借鉴了网上很多搭建环境的方法,也尝试了很多,接下来总结两种个人认为比较简便实用的方法.读者可根据自己的需要自行选择. 方法一:搭建基础 ...

  3. Spring5源码阅读环境搭建-gradle构建编译

      前沿:Spring系列生态十分丰富,涉及到各个方面.但是作为Spring生态的核心基础Spring,是最重要的环节,需要理解Spring的设计原理,我们需要解读源码.   在构建Spring源码阅 ...

  4. 基于Eclipse IDE的Ardupilot飞控源码阅读环境搭建

    基于Eclipse IDE的Ardupilot飞控源码阅读环境搭建 作者:Awesome 日期:2017-10-21 需准备的软件工具 Ardupilot飞控源码 PX4 toolchain JAVA ...

  5. Hadoop源码阅读环境搭建(IDEA)

    拿到一份Hadoop源码之后,经常关注的两件事情就是 1.怎么阅读?涉及IDEA和Eclipse工程搭建.IDEA搭建,选择源码,逐步导入即可:Eclipse可以选择后台生成工程,也可以选择IDE导入 ...

  6. Spring 源码阅读环境的搭建

    前言 本文记录了 Spring 源码环境的搭建方式,以及踩过的那些坑!​当前版本:5.3.2-SNAPSHOT. 环境准备 Git JDK master 分支需要 JDK 11 5.2.x 分支, J ...

  7. Spring源码分析_01_ idea搭建spring源码阅读环境

    二.参考资料 1.Intellij Idea如何导入spring源码

  8. idea构建spring源码阅读环境

    注:由于文章不是一次性完成,下文中的test1目录和test目录应为同一个目录. (一)安装git和Gradle Spring项目托管在github之上,基于Gradle来构建项目.所以要想搭建Spr ...

  9. spring-framework-4.1.x源码阅读环境搭建(导入Eclipse)

    注意:搭建spring-framework-4.1.x源码 eclipse工作空间需要安装jdk8. spring-framework-4.1.x项目采用目前主流的项目管理工具gradle进行构建,至 ...

随机推荐

  1. html中删除表格的一行(有弹窗)

    html中删除表格一行其实很简单,但是加上一个提示弹窗后,点击确定后却获取不到要删除的是哪一行,下面是一个demo html: <tr> <td> <input type ...

  2. css文件分类

    简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...

  3. linux的逻辑运算符

    1:expression :用于计算括号中的组合表达式,如果整个表达式的计算按结果为真,则测试结果也为真. 2:!exp:客队表达式进行逻辑非运算,即对测试结果求反 3:符合 -a 或者 && ...

  4. qt5--文件操作

    文本文件的读写操作: #include "win.h" #include "ui_win.h" #include <QDebug> #include ...

  5. 【leetcode】1233. Remove Sub-Folders from the Filesystem

    题目如下: Given a list of folders, remove all sub-folders in those folders and return in any order the f ...

  6. 1、布局容器Grid、StackPanel、GroupBox、DockPanel、WrapPanel

    Grid——网格布局,其中控件或容器需指定位置 StackPanel——堆叠面板,其中的控件水平布局.竖直布局 DockPanel——停靠面板,内部控件或容器可以放置在上.下.左.右 WrapPane ...

  7. javascript中的原型和原型链(四)

    new运算符工作原理

  8. 51nod 1402 最大值(贪心)

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 思路:借鉴了这篇博文http://blog.csdn.n ...

  9. codevs 5929 亲戚x

                         题目描述 Description 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. ...

  10. 捕获有问题的SQL