Source code acquisition

Because spring- Framework -5.2.0.RELEASE source code needs to be downloaded from abroad, the download speed is too slow, here directly provide baidu connection for everyone to download

Link: pan.baidu.com/s/1bOYb99G1…

Extract code: WQBC – from Baidu network disk super member V6 share

Source code construction and compilation

1. Import the source code into IDEA, you will see the following project structure

The gradle tool is included in this project, and the source code is compiled and can be used directly. Test (Mac and Windows 10)

2. Source code compilation

The source code obtained from this connection is already compiled, so there is no need to compile again. Since you will need a new version of the Spring source code in the future, I will cover the whole process of compiling the source code here. It is better to teach a man to fish than to give him fish.

1) Ali mirror import

A. Click the project and find the build.gradle file in the following directory. Add this to buildScript {} :

repositories{
 maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/' }  
 maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
}
Copy the code

Effect picture:

B. In dependencyManagement{}

repositories {   
mavenCentral()   
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
Copy the code

Replace with:

repositories {   
maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/' }  
maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' } 
mavenCentral()   maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
Copy the code

The renderings are as follows:

2) Enter the following statement in the CONSOLE of IDEA:

gradlew.bat :spring-oxm:compileTestJava

It may take a while, but here’s how it looks:

After compiling, the effect picture is as follows:

BUILD SUCCESSFUL in the 1m 50s prompt So congratulations,spring source compilation is over, ready to embark on your source code reading link!!!!

Three. Source breakpoint operation

1. Build gradle projects

Gradle is used to build projects in Spring source code. So, we need to use Gradle to create our own project. Don’t worry if you haven’t learned Gradle, so skip the steps below. Here, I analyze to give you a method to build Gradle project based on idea tool, zero basis is ok. Consider that you created a Maven project based on Maven 0

1) Right-click the root directory -> Select New-> Click Moudle… , the effect picture is as follows:

2) Select Gradle-> Java -> Next.

Spring ->Name is your gradle project Name ->Location is your project path, default is in the current root directory. The renderings are as follows:

4) When you find your new project in the following directory, congratulations on building gradle project successfully. As shown in the figure:

2. Source breakpoint debugging

1) gradle dependency

Gradle is similar to Maven when the build project succeeds. The required Gradle dependencies are introduced. We are going to read the Spring source code, so we need to introduce spring dependencies

dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile (project (" : the spring - beans ")), the compile (project (" : spring - core ")), the compile (project (" : the spring aop ")) compile(project(":spring-context")) }Copy the code

As shown in the figure:

2) the Main type of building

Presented here is to use, AnnotationConfigApplicationContext annotation way to read the configuration file,

As for using the ClassPathXmlApplicationContext to configuration files, in the form of XML interested friends can try it yourself.

A.S pringConfig. Class class

@Configurationpublic class SpringConfig { @Bean("user") public User getUser() { return new User("xxj", 30); }}Copy the code

B.U ser. Class class

public class User { private String userName; private Integer age; public User() { } public User(String userName, Integer age) { this.userName = userName; this.age = age; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}Copy the code

c.ApplicationAnnotation.class

public class ApplicationAnnotation { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); Object user = context.getBean("user"); System.out.println(user); }}Copy the code

3) Breakpoint demonstration

So far,spring source code before reading the preparatory work, basically complete, next. Breakpoint debug source code phase.

Ctrl + F7, enter:

Spring-framework-5.2.0.release source code compilation and reading and breakpoint debugging to this end.

Long way, may you look happy.