读Spring源码记录(二)--- 迈出艰难的第一步,搭建spring源码阅读环境

我使用mac电脑,编译器是idea,jdk1.8, gradle 4.9

一、下载源码

源码的GitHub地址:https://github.com/spring-projects/spring-framework
可以使用git下载,也可以直接下载zip包,只不过多了一个解压的过程,我git下载不下来,我直接下载的zip包,放在我的idea工作文件目录下:~/IdeaProjects,使用命令unzip spring-framework-master.zip,我想这步都还是比较简单没什么问题。

二、下载gradle

如果使用 brew install gradle 下载gradle,会下载最新的gradle,目前已经是5.5.1的版本,但是spring源码是需要的4开头的版本,我在使用5.5.1版本编译的时候就遇到了报错This version of Gradle requires version 2.0.2 of the build scan plugin or later.这表示当前版本太高了,下图可以检查,提示对应的版本。

gradle版本.png

三、根据源码的提示预编译源码

spring.png

可以从截图中看到,源码里面有相应的导入源码说明,我是导入到idea中,所以根据import-into-idea.md提示进行,可以看到,分为4步,但是实际操作,我发现第一步还需要增加一点。

首先按照文档,到spring-framework-5.0.x目录下,执行./gradlew :spring-oxm:compileTestJava,当然第一次一般都是失败的,我多试了几次就成功了(多是几次==我花了两天下班时间),希望大家有耐心,然后在spring-framework-5.0.x目录下还要执行./gradlew build -x test,这个是因为我后面测试的时候,明明写好了xml文件,可是就是报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'teacher' available,虽然不知道有什么联系,但是没它不行,这个命令的执行也是多试几次,一定要有耐心。
这步完成后,按照文档提示,导入文件File -> New -> Project from Existing Sources,选中你的spring源码目录,选中build.gradle文件。
接下来,重点来了

图中1,2,3,4大家都这么做,图中5的地方大家看其他教程可能发现跟这个有所不同,这个是因为我使用的自己下载安装的gradle,所以是这个路径,如果使用的brew安装的gradle,那么路径应该是/usr/local/Cellar/gradle-5.5.1/libexe
漫长的等待,long long long time。
中间你可能遇到报错:
No such property: values for class: org.gradle.api.internal.tasks.DefaultTas,你点击open file
注释掉最后三行,

接下来就是官方文档常说的spring-aspects模块的问题,可能遇到,可能遇不到,不过最好都执行一下,选中idea中的spring-framework-5.0.x右键 -> Load/Unload modules,将spring-aspects模块移走,然后rebuild一下。
Load/Unload modules.png

最后检查你的源码项目,是否在有点文件中,有的类或者包为红色(无法引入)
这个时候你可以先到无法引入的类的所在模块,执行命令gradle build

如果失败类,那么使用plan B:
就是点它.png

四、添加一个测试模块

我一开始想跟spring统一,使用gradle建立测试模块,我发现我好像不怎么擅长gradle,而且遇到各种问题,为了解决这些问题,我最后使用擅长的maven。
首先选中idea中的spring-framework-5.0.x右键 -> module -> maven,然后就是取名字,next到最后。这是我的测试项目的结构截图。



还要引入相关的spring的模块:file -> project structure



为什么引入这么多模块,得往下看。
然后是代码:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.laoye.spring</groupId>
    <artifactId>my-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Student:

package com.laoye.spring.beans;

public class Student {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Main是空文件,然后测试类
BeansTest:

package com.laoye.test;

import com.laoye.spring.beans.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeansTest {

    @Test
    public void testStudent(){

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application.xml");
        Student student = context.getBean("student",Student.class);
        System.out.println(student.getName());
    }
}

最后是application.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="student" class="com.laoye.spring.beans.Student">
        <property name="name" value="laoye"/>
        <property name="age" value="1"/>
    </bean>
</beans>

一开始我只引入了spring-context,spring-beans,spring-core模块, 然后执行测试代码,会发现我一个问题(解决了好久)

Error:(26, 38) java: 找不到符号
  符号:   类 InstrumentationSavingAgent
  位置: 程序包 org.springframework.instrument

当时没找到什么有效的解决办法,网上都是让重新导入项目,实际解决办法是:
找个这个类所在模块,然后添加到测试模块的依赖中,类InstrumentationSavingAgent在模块spring-instrument中,所以就添加这个模块。


然后在此debug测试用例,会遇到类似的问题,然后就是添加模块,我最后添加的模块很多,如上图所示。

五、总结

路漫漫其修远兮,吾将上下而求索!
遇到问题,查找问题,最后解决问题,这是我们干这一行最基本的方式,希望更多的人能在程序员的道路上坚持下去,也希望有人能一起讨论学习。
终于迈出了艰难第一步。


成功跑完第一个测试用例.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容