instructions

Spring source reading environment configuration is as follows:

  • Spring 5. X version
  • Gradle 4.8.1
  • JDK8
  • IDEA2020.1
  • win10

Set up steps

1. Download Spring source code

Download from: Github link, select 5.x version

If Github downloads too slowly, you can use code cloud acceleration to mirror the address

Another option is to configure the proxy through FQ, which also speeds up the download

2. Configuration Gradle

Spring is built on Gradle. You need to install and configure Gradle first

Run Gradle -v to verify that the configuration is successful. Run Gradle -v to verify that the configuration is successful

3. Build projects

Before precompiling, check the following:

  • Gradle. properties: Check to see if the version is correct
  • JDK version and Gradle version

Execute commands in the code directory:

gradlew :spring-oxm:compileTestJava
Copy the code

Wait until the compilation succeeds. The compilation process may encounter the following problems:

  • An exception occurred plugin Request [ID: ‘com.gradle.build-scan’, version: ‘1.8’]

Solution: Gradle version problem, see this link for details

Gradle Build Tool versions Minimum plugin version Maximum plugin version
> = 6.0 3.0 3.3.1
5.0 5.6.4 2.0.2 3.3.1
4.1-4.10 1.8 1.16
4.0 1.7.4 1.16
2.0-3.5 1.0 1.16
< 2.0 not supported not supported
  • org.gradle.api.CircularReferenceException: Circular dependency between the following tasks: :spring-beans:compileGroovy — :spring-beans:compileJava — :spring-beans:compileKotlin — :spring-beans:compileGroovy (*)

Workaround: Circular reference problem, comment out the next three lines

4. Import the IDEA

File -> New -> Project from Existing Sources -> Find the Spring Project path -> select build.gradle and configure Gradle as shown below

Next idea will build for a long time, download relevant JAR packages, and wait patiently

The following problems may occur:

  • Jar packages are missing: spring-cglib-repack-x.x.x.jar, spring-objenesis-repack-x.x.jar

Solution: Execute the following two commands in spring’s decompression directory

gradle objenesisRepackJar
gradle cglibRepackJar
Copy the code

Debugging code

Create a new Module, write your own code, and walk through spring code.

For module knowledge, please refer to another article: The relationship between Project and Module in IDEA

Right-click project, New->Module

You can also create Maven-based modules as follows:

After creating a module subproject, you need to add module dependencies to facilitate the import of related packages. As follows:

At this point, you’re ready to write code. We create a simple Java bean and print it.

Person.java:

public class Person {
	private String name;

	public String getName(a) {
		return name;
	}

	public void setName(String name) {
		this.name = name; }}Copy the code
Beans.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-3.0.xsd">

	<bean id="person" class="org.wds.Person">
		<property name="name" value="zhangsan"/>
	</bean>
</beans>
Copy the code
Main.java:

public class Main {

	public static void main(String[] args) {
		ApplicationContext context =
				new ClassPathXmlApplicationContext("Beans.xml");
		Person obj = (Person) context.getBean("person"); System.out.println(obj.getName()); }}Copy the code

The running result is as follows:

At this point, the environment is set up successfully, and the source code can be debugged and read through the break point.