如何本地下载阅读Spring源码

环境准备

由于spring源码使用GRADLE工具构建,所以本地必须要安装好gradle环境

gradle安装与idea集成

  1. 官网下载即可,配置环境变量与java的配置相同,详情请百度。
    官网下载地址:https://gradle.org/releases/

  2. 集成到idea中
    在这里插入图片描述

  3. 本地gradle优先使用本地maven仓库配置
    在这里插入图片描述

注意:**GRADLE_USER_HOME**不能写错。

idea创建gradle项目使用阿里云镜像

plugins {
    
    
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    
    
    maven {
    
     url "http://maven.aliyun.com/nexus/content/groups/public" }
    mavenLocal()
    mavenCentral()
}

dependencies {
    
    
    testCompile group: 'junit', name: 'junit', version: '4.12'
    runtimeOnly group: 'org.jetbrains.kotlin', name: 'kotlin-compiler-embeddable', version: '1.3.72'

}

下载Spring源码并启动运行项目

下载spring源码到idea

1.直接在github下载即可,导入到idea过程忽略
github源码地址:https://github.com/spring-projects/spring-framework.git
注意选择所需要调试的版本
5.3.x需要高于JDK8以上的jdk版本,5.2.x支持JDK8及以下的版本

调整Repositories的本地仓库以及阿里云镜像

在这里插入图片描述

创建自己的测试module

1.记得将bulid.gradle文件名修改成项目名一样的前缀,如:spring-debug.gradle
在这里插入图片描述
spring-debug.gradle配置如下

description = "Spring Debug"
group 'org.springframework'
version '6.0.0-SNAPSHOT'


/*repositories {

    mavenCentral()
}*/

dependencies {
    
    
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
//    compile(project(":spring-tx"))
    compile(project(":spring-context"))
//    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}


在项目中创建xml,配置bean

在这里插入图片描述
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" xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean id="a" class="org.clj.test.A" />
</beans>

编写java代码并测试

package org.clj.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
 * @Description 
 * @Date 2022/2/3 21:36
 * @Author chenglj
 **/
public class A {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("running:===============");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		A bean = context.getBean(A.class);
		System.out.println(bean);
	}
}

能够输出bean则说明本地spring项目运行成功