Spring源码环境搭建
测试环境
- macOS
- JDK11
- Gradle 6.8+
- spring分支 5.3.x
准备JDK和Gradle
通过oracle官网下载JDK11并安装,~/.zshrc设置环境变量
export JAVA_11_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.15.1.jdk/Contents/Home
export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home
alias jdk8='export JAVA_HOME=$JAVA_8_HOME'
alias jdk11='export JAVA_HOME=$JAVA_11_HOME'
Gradle官网 下载并配置环境变量
GRADLE_HOME=/Users/sunhongyue/work/code/3rd/gradle-7.4.2;
export GRADLE_HOME
export PATH=$PATH:$GRADLE_HOME/bin
准备源码
fork spring的官方仓库,并clone到本地
在终端clone的时候最好设置代理,这样会快很多,查看自己的代理配置,并在~/.zshrc添加代理配置
# where proxy
proxy () {
export all_proxy=http://127.0.0.1:1087
echo "All Proxy on"
}
# where noproxy
unproxy () {
unset all_proxy
echo "All Proxy off"
}
配置好代理之后,速度飞起
spring目前是用Gradle进行构建,相比Maven,Gradle构建大型项目时会快很多
切换新的分支
➜ spring-framework git:(main) git checkout -b 5.3.x.note origin/5.3.x
Branch '5.3.x.note' set up to track remote branch '5.3.x' from 'origin'.
Switched to a new branch '5.3.x.note'
我们切到自己的分支进行开发和测试。
预编译
根目录下的
import-into-idea.md
文件给出了步骤,第一步就是预编译
首先安装gradle,版本需要6.8+
gradle -v
gradle :spring-oxm:compileTestJava
这里可能会报错
> Task :spring-core:compileJava
/Users/sunhongyue/work/code/github/java/MySpring2/spring-framework/spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
^
这里isAccessible() JDK9之后就弃用了,所以在源码中把这句method.isAccessible() 去掉了,之后就可以成功预编译了
编译成功
➜ spring-framework git:(5.3.x.note) ✗ gradle :spring-oxm:compileTestJava
> Task :spring-beans:compileGroovy
注: /Users/sunhongyue/work/code/github/java/MySpring2/spring-framework/spring-beans/build/tmp/compileGroovy/groovy-java-stubs/org/springframework/beans/factory/groovy/GroovyDynamicElementReader.java使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
> Task :spring-oxm:xjcGenerate
Errors occurred while build effective model from /Users/sunhongyue/.gradle/caches/modules-2/files-2.1/com.sun.xml.bind/jaxb-xjc/2.2.11/4da3d39ae8ccc39549e3f8971f56035f61d7bf79/jaxb-xjc-2.2.11.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${tools.jar} in com.sun.xml.bind:jaxb-xjc:2.2.11
Errors occurred while build effective model from /Users/sunhongyue/.gradle/caches/modules-2/files-2.1/com.sun.xml.bind/jaxb-core/2.2.11/db0f76866c6b1e50084e03ee8cf9ce6b19becdb3/jaxb-core-2.2.11.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${tools.jar} in com.sun.xml.bind:jaxb-core:2.2.11
Errors occurred while build effective model from /Users/sunhongyue/.gradle/caches/modules-2/files-2.1/com.sun.xml.bind/jaxb-impl/2.2.11/2d4b554997fd01d1a2233b1529b22fc9ecc0cf5c/jaxb-impl-2.2.11.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${tools.jar} in com.sun.xml.bind:jaxb-impl:2.2.11
BUILD SUCCESSFUL in 35s
47 actionable tasks: 29 executed, 18 up-to-date
A build scan was not published as you have not authenticated with server 'ge.spring.io'.
添加测试module进行测试
创建一个新的module
build.gradle的dependencies块添加如下内容,引入同级的module
implementation(project(":spring-beans"))
implementation(project(":spring-core"))
implementation(project(":spring-context"))
resource目录下创建spring-config.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: hello-->
<bean id="hello" class="com.hongyi.Hello"/>
</beans>
创建Hello.java测试
文件开头要添加License信息
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hongyi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Hello {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
Hello hello = (Hello) applicationContext.getBean("hello");
hello.test();
}
public void test() {
System.out.println("你好");
}
}
运行main测试结果
使用gradle构建module
gradle :spring-samples:build
SpringBoot环境搭建
测试环境
- macOS
- JDK8+
- Gradle 6.8+
- spring-boot分支 2.4.x
准备源码
fork spring-boot的官方仓库
在终端clone的时候同样最好设置代理,设置代理见上文。
切换新的分支
git checkout -b 2.4.x.note origin/2.4.x
我们切到自己的分支进行开发和测试
运行测试程序
Spring-boot工程里给出了很多smoke test,
关于smoke test,这个词语源自硬件行业,可以类比新电路板的基本功能检查(即新电路板焊好之后先进行通电检查,如果存在设计缺陷,电路板可能会因为短路而冒烟)。冒烟测试,也被称为构建验证测试或构建验收测试,它确定一个程序的最关键功能是有效的,但没有深入到更细的细节。冒烟测试是在构建之后和发布之前对软件进行的初步检查。这种类型的测试在关键测试实现之前发现应用程序中的基本和关键问题。
准备好代码之后,用IDEA导入项目,会经历一个下载依赖,build,索引的过程,等就绪后,找到spring-boot-build.spring-boot-tests.spring-boot-smoke-tests.spring-boot-smoke-test-tomcat,这是一个简单的基于tomcat的web程序,默认监听8080端口,运行起来后,浏览器输入
http://localhost:8080/
可验证结果。