Spring 6 source code compilation tutorial sharing~

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 6needJDK 17
![Insert picture description here](https://img-blog.csdnimg.cn/08f8d33b8ca64556a6603b2c7c538f3f.png

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.

  • Add a new system variable of 17

insert image description here

  • Change the value of the variable name of the original JAVA_HOME as follows to switch the Java version

insert image description here

  • 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.

insert image description here

Check whether the configuration is good:java-version

insert image description here

3. Install Gradle

1. Download Gradle

insert image description here

2. Configure environment variables

Add system variable: GRADLE_HOME=D:\Develop Software\gradle-7.6

insert image description here

Add it to Path (%GRADLE_HOME%\bin), and then confirm it all the time.

insert image description here

view versiongradle -v

insert image description here

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

insert image description here
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

# 将远程项目镜像拉到本地
git remote add spring [email protected]:mirrors/spring-projects/spring-framework.git
# 切换分支
git checkout -b v6.0.2
# 拉取分支到本地
git pull origin v6.0.2

insert image description here

2. IDEA environment settings

  • File → Settings → Build,Execution,Deployment → Build Tools → Gradle

insert image description here

  • build.gradle

Find the repositoriesconfiguration node and add the address of the Alibaba Cloud mirror warehouse

maven {
    
     url "https://maven.aliyun.com/repository/public" } // 阿里云镜像仓库

insert image description here

  • settings.gradle
    Find the repositoriesconfiguration node and add the address of the Alibaba Cloud mirror warehouse
maven {
    
     url "https://maven.aliyun.com/repository/public" } // 阿里云镜像仓库

insert image description here

  • gradle.properties
    gradle.propertiesAdd the java path to the configuration file in the project
    org.gradle.java.home=C:\\Program Files\\Java\\jdk-17insert image description here
  • Slowly wait for gradle to pull related dependenciesinsert image description here
  • 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.
    insert image description here
  • 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!
    insert image description here
    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
    insert image description here
    insert image description here
  • After the pull is complete, it means OKinsert image description here

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:

insert image description here

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:compileTestJavafirst :
insert image description here

done:

insert image description here

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.gradleClick OK to complete the compilation
    insert image description here
3.When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)

insert image description here

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

insert image description here

insert image description here

2. Add dependencies

The new dependency under spring-mytest-samplethe module , which includes the dependencies of the Spring runtime context such as , and IoC container.build.gradlespring-contextspring-corespring-bean
insert image description here

3. Test code

The code structure is as follows:

insert image description here

/**
 * 车接口
 */
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());
	}
}

4. Test results

insert image description here

Perfect ending! end…

Guess you like

Origin blog.csdn.net/qq_40436854/article/details/129889129