Why read the source code? That’s an interesting question, sort of like, why read a book? Why climb mountains?

This is also a philosophical question, and I think everyone has a different answer, but here are some thoughts on the benefits of reading source code. (PS: You are welcome to add in the comments section.)

The benefits of reading the source code

1. Know what is and why

If anything, IT’s important to know the core principles and execution processes of open source software, which can give us valuable time to quickly locate and fix problems.

2. Make yourself better

Learning source code can let us stand on the shoulders of giants, you can learn excellent coding skills and clever design ideas in the source code, as well as the ground application of design patterns, there are some classic coding norms and naming rules. To constrain and improve their own programming code, to write better code.

Get a better job

In the increasingly competitive interview, especially in the interview of the large factory, for the investigation of the source code is very important, only understand and familiar with the source code, in order to get the offer they want.

So with all the benefits of reading the source code, how do we get started?

How to read the source code?

First we need to download the Spring source code, then import the Spring source code into the IDE, then compile the Spring source code, and then add the test code by adding modules. By introducing the Spring source section into the Module, we can use the debugging function to go inside the Spring framework and see the method execution flow and the implementation of the source code.

Spring source code reading process, as shown below:

Build Spring source code reading environment

Window 10 / JDK 8 / Spring 5.2.2 / IDEA 2019

Note: JDK 8 and above is required after Spring 4.x.

1. Install the Gradle

We need to install Gradle before we can start, because Spring is built on Gradle.

Gradle is an open source JVM-based build tool similar to Maven. The project has been built through three eras: Apache Ant (circa 2000), Maven (2004), and Gradle (circa 2012), where Gradle has a simpler configuration, higher performance, and better user experience (IDE support code hints) than Maven.

For example, when using Maven, the configuration file is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0 the SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Copy the code

For Gradle, the configuration is as follows:

Application plugin:' Java 'group='com.mycompany.app' archivesBaseName='my-app' version=' 1.0-snapshot' repositories{ MavenCentral ()} dependencies{testCompile 'junit:4.13'}Copy the code

Hibernate, Spring and other excellent open source projects have migrated their projects from Maven to Gradle. Android Studio, Google’s official IDE for Android development, is also built using Gradle by default. But there’s no denying that Maven is still the dominant way Java projects are built today.

Gradle and Maven performance tests, as shown below:

To learn more about Gradle vs Maven, visit Gradle at gradle.org/maven-vs-gr…

First, we need to download the Gradle installation package from gradle.org/releases/

Select binary-only installation package and download it.

Note: Gradle 5.X is required. The latest release of Spring does not support Gradle 6+. If the 6+ version is installed, an error message is displayed as follows:

The build scan plugin is not compatible with Gradle 6.0 and later. Please use The Gradle Enterprise plugin instead. Open File

Once the download is complete, we need to unzip Gradle to a specified directory, such as “C:\Gradle\ Gradle”.

Once the decompression is complete, we need to configure two system variables.

Right-click computer -> Properties -> Advanced System Settings -> Environment Variables, in the system variables area, first click “New” input variable name: GRADLE_HOME, variable value: C:\gradle\gradle-5.6.4 (according to your own path); Find the Path environment variable and add %GRADLE_HOME%\bin.

At this point, Gradle installation is complete. Let’s use the command line execution tool to test Gradle installation successfully.

Open a new CMD command window, enter the command gradle -v, if the version message is displayed, the configuration is successful, as follows:

C: \ Users \ stone > gradle -v -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- gradle 5.6.4 ------------------------------------------------------------ Build time: 2019-11-01 20:42:00 UTC Revision: Dd870424f9bd8e195d614dc14bb140f43c22da98 Kotlin: 1.3.41 Groovy: 2.5.4 Ant: Apache Ant(TM) Version 1.9.14 compiled on March 12 2019 JVM: 1.8.0_211 (Oracle Corporation 25.211-B12) OS: Windows 10.0 amd64 10Copy the code

If the previous version information is displayed, Gradle has been installed properly.

Gradle acceleration

The default directory is C: users\username.gradle. The default directory is C:\Users\ username.gradle. If not, create a new init.gradle file and add the following configuration:

allprojects {
   repositories {
       maven {
           url "http://maven.aliyun.com/nexus/content/groups/public"
       }
   }
}
Copy the code

2. Download the latest version of Spring source code

Spring source address: github.com/spring-proj…

Tip: It’s best to Fork a project, because you can modify the source code and add comments to your repository so you can document and manage your own learning of Spring source code.

Source code download acceleration

Because of the large number of Spring source files, if you use the original address of Spring to download it, it will take a long time, or even fail to complete it for many times. At this time, we can use the address on gitee to download it much faster. Yards cloud address to https://gitee.com/mirrors/Spring-Framework

As shown in the figure below, use the official Spring source address and synchronize once a day.

3. Import and compile Spring

After importing the source code, you can edit the settings.gradle file under the root directory of Spring source code and add the source configuration information of Ali:

Maven {url “maven.aliyun.com/nexus/conte…” }

The configuration is shown in the figure below:

Next we can Import Spring source code, click Import Project, select the path of Spring source code, select Gradle method Import, as shown below:

Click Next and select the local Gradle path as shown below:

Click Finish to start the automatic build phase of the project.

The Spring build is completed (taking a long time), as shown below:

By now, the Spring source code has been built.

Debugging Spring source code

First we will add a New test project to the Spring source project and click New -> Module… Create a Gradle Java project as shown below:

After creating it, we add a dependency to the Spring source code in build.gradle:

api(project(":spring-context"))
Copy the code

As shown below:

Next, we need to create a bean and configuration file (application.xml) and startup file (myApplication.java) in the project.

The bean implementation code is as follows:

public class Person {
	private Integer id;
	private String name;

	public Integer getId(a) {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName(a) {
		return name;
	}

	public void setName(String name) {
		this.name = name; }}Copy the code

The code for starting the MyApplication class is as follows:

import org.springframework.beans.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApplication {
	public static void main(String[] args) {
		ApplicationContext context =
				new ClassPathXmlApplicationContext("classpath*:application.xml");
		Person person = context.getBean("person", Person.class); System.out.println(person.getName()); }}Copy the code

The configuration file application.xml (in Resources) is configured as follows:

<?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 id="person" class="org.springframework.beans.Person">
		<property name="id" value="1"/>
		<property name="name" value="Java"/>
	</bean>
</beans>
Copy the code

Now we can start and debug the Spring source code.

conclusion

The Spring source code is not as difficult to read as we might think. Installing the Debug environment for Spring source code will only help you understand the flow of Spring execution. We simply need to download the source code of Spring, install Gradle environment, import it into the compiler to compile the Spring source package, and finally create a Java project in the Spring source project to add a reference to the Spring module, and then debug and execute the Spring source code. This allows us to get inside Spring and read the relevant source code.

For more exciting content, please follow the wechat public account “Java Chinese Community”.