Spring学习笔记-02-Spring源码编译
1、环境准备
1、OpenJDK-1.8.0_342-342 下载链接
2、IDEA2021.3
3、gradle-7.2-bin (gradle版本根据Spring配置文件选择,下文有介绍)下载链接
2、插件下载
IDEA中下载插件 AspectJ、Kotlin
3、从GitHub拉取Spring源码(本文使用Spring-5.3.x)
源码地址:spring-projects/spring-framework: Spring Framework
若使用Spring其他版本,关于jdk版本的对应关系,可以参考:Spring Framework与JDK版本对应关系
在GitHub下载太慢,可以将GitHub仓库克隆到Gitee仓库,步骤如下:
1、登录GitHub,根据地址打开Spring仓库,选择版本
2、获取Git链接
3、登录Gitee,准备克隆GitHub仓库
4、添加Git链接,等待克隆
5、从Gitee获取Git链接
6、将Spring源码克隆到本地
4、更改配置文件
1、打开 spring-framework-5.3.x\gradle\wrapper\gradle-wrapper.properties 获取gradle版本,并下载到本地(gradle安装配置教程),将配置文件中gradle下载地址替换为本地地址(如果不替换,每次从新构建项目都回从远程下载)
distributionUrl=file:///E:/Gradle/gradle-7.2-bin.zip
2、打开build.gradle,将jar包下载地址替换问阿里云镜像仓库(加快jar包下载速度)
repositories {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven {
url 'https://maven.aliyun.com/repository/spring/'
}
mavenLocal()
mavenCentral()
gradlePluginPortal()
maven { url "https://repo.spring.io/release" }
}
3、打开setting.gradle,替换阿里仓库,并注释 id “io.spring.ge.conventions” version “0.0.11” (不注释会要求进行企业认证,不影响代码构建,但会有报错信息)
repositories {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven {
url 'https://maven.aliyun.com/repository/spring/'
}
mavenLocal()
mavenCentral()
gradlePluginPortal()
maven { url "https://repo.spring.io/release" }
}
5、开始构建
查看官方构建文档
1、根据官方文档,先通过cmd打开源码根目录,然后执行命令(windows系统去掉 ./ )
./gradlew :spring-oxm:compileTestJava
显示绿色的BUILD SUCCESSFUL就是构造成功了
此处一般不会有问题,如果显示 没有git仓库 的问题,不用理会,如果强迫症,将项目添加到git仓库进行管理就可以解决了
2、将项目导入idea,选择build.gradle
3、导入成功后idea会开始构建,此时停止构建,更改配置
4、重新开始构建,显示如下就是构建成功
如果构建失败,报出如下错误:
请检查所有配置过阿里云镜像的地方,将镜像地址中的 http 更改为 https 就可以了,包括gradle中的配置init.gradle配置文件
Execution failed for task ':buildSrc:compileJava'.
> Could not resolve all dependencies for configuration ':buildSrc:compileClasspath'.
> Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'Bstek(http://nexus.bsdn.org/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
5、开始编译整个项目
显示如下就是编译成功
如果报出关于 spring-aspect包无效 的错误,将spring-context模块移除重新编译,操作如下:
6、测试
在此我提供两个测试类,添加到 spring-context模块下
package test.mytest01;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author:
* @Description:
* @Date: 2022/9/21
*/
@Configuration
@ComponentScan
public class Config {
@Bean
public User user(){
return new User();
}
}
package test.mytest01;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author:
* @Description:
* @Date: 2022/9/21
*/
public class MyTest {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User user = context.getBean("user", User.class);
System.out.println(user);
}
}
package test.mytest01;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author: lxb
* @Description:
* @Date: 2022/9/21
*/
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("zhangsan")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
如果最后输出结果,就可以开始你的Spring源码学习之旅了