大家好,我是极客涛,不知道小伙伴有没有和我一样的情况,在阅读Spring源码时,只通过静态的代码阅读很难有更深刻的理解,所以建议通过写测试类进行debug的方式,对核心的代码进行运行时的状态调试,这样可以更容易理解,本文介绍了如何搭建Spring的本地编译环境。笔者亲试,只要按照步骤来一定可以顺利搭建完成。
使用版本:
gradle-6.4.1-all.zip (官网地址)
Spring-framework-master (官网地址)
vim ~/.bash_profile
export M2_HOME=/Users/wangxt/workspace/software/apache-maven-3.9.2
export GRADLE_HOME=/Users/wangxt/workspace/software/gradle-6.4.1
export PATH=$PATH:$M2_HOME/bin:$GRADLE_HOME/bin
source ~/.bash_profile
gradle -v
------------------------------------------------------------
Gradle 6.2.2
------------------------------------------------------------
Build time: 2020-03-04 08:49:31 UTC
Revision: 7d0bf6dcb46c143bcc3b7a0fa40a8e5ca28e5856
Kotlin: 1.3.61
Groovy: 2.5.8
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 1.8.0_371 (Oracle Corporation 25.371-b11)
OS: Mac OS X 13.3 x86_64
gradle wrapper
maven { url 'https://maven.aliyun.com/repository/public' }
加完效果
pluginManagement {
repositories {
maven { url 'https://maven.aliyun.com/repository/public'}
gradlePluginPortal()
maven { url 'https://repo.spring.io/plugins-release' }
}
}
将 org.gradle.jvmargs的值修改为 -Xmx2048M; 并添加 org.gradle.daemon=true
version=5.3.0-SNAPSHOT
org.gradle.jvmargs=-Xmx2048M
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.daemon=true
检索关键词“repositories”
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
加完效果
repositories {
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://repo.spring.io/snapshot" } // Reactor
maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" } // RSocket
}
删完效果
pluginManagement {
repositories {
maven { url 'https://maven.aliyun.com/repository/public'}
gradlePluginPortal()
maven { url 'https://repo.spring.io/plugins-release' }
}
}
plugins {
id "com.gradle.enterprise" version "3.2"
}
删除版本号中的SNAPSHOT
imports {
mavenBom "com.fasterxml.jackson:jackson-bom:2.11.0"
mavenBom "io.netty:netty-bom:4.1.50.Final"
mavenBom "io.projectreactor:reactor-bom:2020.0.0-SNAPSHOT"
mavenBom "io.rsocket:rsocket-bom:1.0.1"
mavenBom "org.eclipse.jetty:jetty-bom:9.4.29.v20200521"
mavenBom "org.jetbrains.kotlin:kotlin-bom:1.3.72"
mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.3.5"
mavenBom "org.junit:junit-bom:5.6.2"
}
修改**spring-framework-master\buildSrc\src\main\java\org\springframework\build\compile\CompilerConventionsPlugin.java 文件,删除“-Werror”**参数
编译时间一般几分钟,只要没有报异常,等着就行
# 编译oxm模块
./gradlew :spring-oxm:compileTestJava
# 编译core模块
./gradlew :spring-core:compileTestJava
3.1创建模块
3.2 修改 build.gradle 配置文件
plugins {
id("java")
}
group = "org.springframework"
version = "5.3.0-SNAPSHOT"
repositories {
mavenCentral()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
}
dependencies {
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
4.1创建测试bean
public class TestBean {
private String name = "hello, 你好";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.2 添加xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="testBean" class="org.springframework.wangxt.TestBean"/>
</beans>
4.2 运行测试代码
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
String name = classPathXmlApplicationContext.getBean(TestBean.class).getName();
System.out.println(name);
}
}
运行结果
> Task :spring-wangxt-test:Main.main()
hello, 你好