Hello, everyone! I am Nai Nai, a little fat man who moves bricks to make Web IDE in a factory!

Today, let’s set up a Spring debugging environment. The goal is to start the Spring container, parse the XML configuration, and create Bean objects.

Why XML configuration and not annotations?

The logic of declaring beans using XML configuration is clear and relatively easy to understand.

Video can be seen B station: www.bilibili.com/video/BV1yh…

😝 Nai nai is shy, we give a three support, Thanksgiving 1024~

1. Rely on tools

1.1 the IDEA

Jetbrains IDEA tool, of course, I believe that most fat people use it.

Nai Nai uses 2020.3 version, fat friends try to ensure not lower than this version ha.

1.2 the JDK

You need to compile the Spring code using JDK, and Here Nai is using JDK 1.8

$ java -version
java version "1.8.0 comes with _144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
Copy the code

2. Source code pull

Use IDEA from the official repository github.com/spring-proj… Cloning projects.

Friendly tip: if the network is not a good fat friend, you can choose to use the mirror warehouse provided by Gitee gitee.com/mirrors/Spr…

Even with Gitee’s image, it takes several minutes to clone and the code is around 100 MB!!

Here, the Spring version we are using is 5.3.10-snapshot.

Friendship tip: Fat friends can consider Fork under the official warehouse, why?

Now that we’ve started reading and debugging the source code, we’ll probably write some comments, have our own repository, and be free to commit. 😜

3. Download dependencies

① After cloning the Spring project, IDEA will automatically download the Gradle tool you need. As shown below:

Here, we are using Gradle version 6.8.3.

Friendship tip: because it is downloaded from foreign websites, so need to wait patiently for a while, Nai – nai spent more than 2 minutes.

After downloading Gradle, IDEA will use it to automatically download the dependent libraries. As shown below:

Since Gradle support using Maven rely on, so we can use ali cloud Maven mirror https://maven.aliyun.com/nexus/content/groups/public/ * *. Modify the build.gradle file as shown below:

The effect was very good, but It took Nai Nai 10 minutes to download the dependency. The main reason is that Spring integrates many frameworks, which introduces a lot of dependencies.

Ps: If the intermediate download fails, click the “green Refresh” button to continue downloading dependencies.

4. Debug the Spring example

After downloading the dependencies, we can debug the Spring source code. Although Spring does not provide the Example project directly, we can see the execution flow of Spring by debugging the unit test class provided by Spring.

For example:

(1) through the Debug run XmlBeanDefinitionReaderTests class # withFreshInputStream (), the method of debugging the Spring to parse the XML to cooperate, get the Bean definition. As shown below:

Later, you can learn more by debugging the source of the XmlBeanDefinitionReader class.

Tip: Spring first parses to the Bean definition and then creates the Bean object. 🙂

(2) run through the Debug ClassPathXmlApplicationContextTests class # testSingleConfigLocation (), the method of debugging the Spring container initialization process, including the creation of a Bean. As shown below:

Follow-up, fat friends can debug ClassPathXmlApplicationContext class source code, learn more.


As an additional tip, if we read the Spring source and find that a branch of logic is missing, we can break it and unit test the entire directory. As shown below:

For more spring-related articles, check out the Spring Framework implementation principles and source code analysis.

For more Spring Boot related articles, you can check out Spring Boot implementation principle and source code analysis.

For more Spring Cloud related articles, check out Spring Cloud Implementation principles and source code analysis.