Foreword: The Spring Boot 3 RELEASE version was officially released on November 24, 2022. This Spring Boot milestone upgrade also requires a minimum of JDK 17 and Spring Framework 6, and its core framework Spring also ushered in on November 16, 2022. A major version upgrade from 5.3.x to 6.0.x, take this opportunity to write a tutorial sharing about Spring 6 source code compilation.
1. Environmental statement
Spring source code compilation official document: https://github.com/spring-projects/spring-framework/wiki/Build-from-Source
According to the official description, Spring 6
needJDK 17
basic environment | Version | local path |
---|---|---|
operating system | win 10 | - |
spring source code | 6.0.2 | D:\Source Code |
Java environment | JDK 17 | C:\Program Files\Java\jdk-17 |
build tool | Gradle 7.6 | D:\Develop Software\gradle-7.6 |
development tools | 2021.3.2 | - |
2. Install JDK 17
1. Download JDK17
Download link: https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe
After downloading, install it silently, and modify the JDK path as needed (I put it under C:\Program Files\Java\jdk-17)
2. Configure environment variables
Configure according to your own situation. If the local JDK version is not 17, you need to configure it, which is ignored by JDK 17!
Considering that users who originally used JDK 8 may be involved, you need to configure more JDK 17.
-
Change the value of the variable name of the original JAVA_HOME as follows to switch the Java version
-
Then edit the system variable Path, and configure A in the figure below. At the same time, you need to ensure that the two variables at B are moved down below A (the environment variable is read from top to bottom by default, and moved down to prevent the system from reading the default Java. Version), after the configuration is complete, just keep confirming.
Check whether the configuration is good:java-version
3. Install Gradle
1. Download Gradle
-
Download address: https://gradle.org/releases
-
Download and unzip to the specified directory (mine is D:\Develop Software\gradle-7.6)
2. Configure environment variables
Add system variable: GRADLE_HOME=D:\Develop Software\gradle-7.6
Add it to Path (%GRADLE_HOME%\bin), and then confirm it all the time.
view versiongradle -v
3. Configure the mirror warehouse
Create a new init.gradle file in the gradle installation location (D:\Develop Software\gradle-7.6\init.d) directory
Refer to Alibaba Cloud official gradle configuration guide: https://developer.aliyun.com/mvn/guide, the complete content of init.gradle is as follows:
allprojects {
repositories {
maven {
url 'file:///D:/Develop Software/maven/repository'} // 本地仓库地址,如果没有依次向下寻找
maven {
url "https://maven.aliyun.com/repository/public" }
mavenLocal()
mavenCentral()
}
buildscript {
repositories {
maven {
url 'https://maven.aliyun.com/repository/public' }
mavenLocal()
mavenCentral()
}
}
}
4. Source code compilation
1. Get Spring source code
- You can go to GitHub to download https://github.com/spring-projects/spring-framework.git (the speed may be slower)
- I download https://gitcode.net/mirrors/spring-projects/spring-framework.git directly on GitCode here , and pull it directly through the command
git clone [email protected]:mirrors/spring-projects/spring-framework.git
- Use IDEA to open the pulled source code and select the stable version v6.0.2, so you need to switch branches:
# 将远程项目镜像拉到本地
git remote add spring [email protected]:mirrors/spring-projects/spring-framework.git
# 切换分支
git checkout -b v6.0.2
# 拉取分支到本地
git pull origin v6.0.2
2. IDEA environment settings
Find the repositories
configuration node and add the address of the Alibaba Cloud mirror warehouse
maven {
url "https://maven.aliyun.com/repository/public" } // 阿里云镜像仓库
-
settings.gradle
Find therepositories
configuration node and add the address of the Alibaba Cloud mirror warehouse
maven {
url "https://maven.aliyun.com/repository/public" } // 阿里云镜像仓库
-
gradle.properties
gradle.properties
Add the java path to the configuration file in the project
org.gradle.java.home=C:\\Program Files\\Java\\jdk-17
-
Slowly wait for gradle to pull related dependencies
- Note that an error similar to this may appear in the process of pulling dependencies: it says that the relevant jar is not found. At this time, we need to go to the maven official website to search for the relevant version of the jar package, download it and throw it into the directory in the prompt.
- At the same time, an error similar to this may also occur: the corresponding method was not found.
This may be caused by not switching the Spring branch. The code of the Spring 5.x branch version is still used, and Gradle 7 has changed the corresponding method name, resulting in failure to find it. Report an error to the method!
Solution:
Solution 1: Switch the Spring version to 6.x;
Solution 2: You can update the corresponding table with the method provided by the Gradle official website, and replace it accordingly
-
After the pull is complete, it means OK
3. Compilation steps
After completing the above source code import and related settings, you can compile the source code.
Refer to the IDEA import documentation import-into-idea.md, only three steps are required:
1. Precompile spring-oxm
with ./gradlew :spring-oxm:compileTestJava
Enter in the Terminal terminal of IDEA to execute the precompilation of spring-oxm ./gradlew :spring-oxm:compileTestJava
first :
done:
2.Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
- File → New → Project from Existing Sources → Select File or Directory to import
build.gradle
Click OK to complete the compilation
3.When prompted exclude the spring-aspects
module (or after the import via File-> Project Structure -> Modules)
5. Test case
After compiling the Spring source code above, we can add a sample module to rely on other spring modules in the project to do a simple test.
1. Create a new module
File → Module Add spring-mytest-sample sample module
2. Add dependencies
The new dependency under spring-mytest-sample
the module , which includes the dependencies of the Spring runtime context such as , and IoC container.build.gradle
spring-context
spring-core
spring-bean
3. Test code
The code structure is as follows:
/**
* 车接口
*/
public interface CarService {
// 获取车的颜色
String getColor();
// 获取车的品牌
String getBrand();
}
/**
* 奔驰
*/
@Service
@Primary // 当一个接口存在多个实现类时,通过@Primary注解来指明哪个实现类作为首选进行自动装配注入
public class BenzCarServiceImpl implements CarService {
@Override
public String getColor() {
return "Color: black";
}
@Override
public String getBrand() {
return "Brand: Benz";
}
}
/**
* 保时捷
*/
@Service
public class PorscheCarServiceImpl implements CarService {
@Override
public String getColor() {
return "Color: pink";
}
@Override
public String getBrand() {
return "Brand: Porsche";
}
}
/**
* 启动测试类
*/
@ComponentScan("com.spring.mytest.sample.**")
public class SpringMyTestSampleApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
SpringMyTestSampleApplication.class
);
CarService carService = context.getBean(CarService.class);
System.out.println(carService.getColor());
System.out.println(carService.getBrand());
}
}