This example is compiled based on spring-framework-5.2.22.RELEASE + GradleWapper + jdk1.8.0_131
# Environment preparation
maven
jdk8+
idea
# Source code download
Spring's source code is published on github
Download the latest release version source code
Don’t worry too much about version differences. There is no need to deliberately guarantee the same version as the teacher, as long as the official release of 5.x (RELEASE) is enough , because the core branch of Ioc AOP in Spring will not change, only minor details will change.
M:
The M in M1, M2,... is the abbreviation of milestone, which means milestone and represents a version with major improvements.
git address()
git clone --branch v5.2.22.RELEASE https://github.com/spring-projects/spring-framework.git
Build tool preparation:
Install the gradle version corresponding to the source code (or not install it). It is recommended to use gradle in gradleWraper.
Gadle introduction:
Gradle is a build system that simplifies your compilation, packaging, and testing processes. Students who are familiar with Java can compare Gradle to Maven.
The function of Gradle Wrapper is to simplify the installation and deployment of Gradle itself. Different versions of projects may require different versions of Gradle. Manual deployment is troublesome and may cause conflicts, so you need Gradle Wrapper to help you handle these things. Gradle Wrapper is part of the Gradle project.
Gradle does not need to spend time to learn in depth, because the process of learning the source code will not involve too much Gradle. Of course, if you are interested, you can learn it. It is equivalent to a rising star, but Maven is already good enough, and there should be no way to replace Maven for the time being.
gradleWraper is reflected in this file, which is equivalent to automatically downloading gradle remotely to the local (so you can download gradle, or not, because you can use the unified remote version of gradleWraper):spring-framework-5.2.22.RELEASE\gradle\wrapper\gradle-wrapper.properties
So if you need to download, it is best to download the gradle version corresponding to this link
spring-framework\gradle\wrapper\gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Modify build.gradle
This file is equivalent to our Maven pom.xml to manage project dependencies and other information...
In the root directorygradle.properties
Set up mirroring
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" }
}
Compile according to the official website:
Use gradlew (gradle-wrapper command) to compile oxm:compileTest Java first: Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
- Compile compileTestJava module
Open the folder where the source code is located and enter it in the windows cmd command. There is no need to enter "./" in the current directory in windows. (may also be entered)
gradlew :spring-oxm:compileTestJava
After configuring the image, the compilation will be completed quickly (a minute or two). If it takes a long time, it means that your image is not working. If the compilation is abnormal, please search it yourself. Abnormalities may occur in everyone's computer and environment.
- Import the project into idea
Import the project into idea: Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
After importing, wait for compilation. Don’t worry, it will take a little time and compilation is completed:
- Add test module code:
Add test code: Code away
new->model->gradle-->Enter module name
Write code to test ioc functionality
- Add dependencies
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile(project(":spring-context"))
}
compile(project(":spring-context")) represents this project
- Feel free to add any beans:
package org.springframework.bean;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl {
public void hello(){
System.out.println("Hello Spring");
}
}
- Add startup configuration class:
package org.springframework.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.springframework.bean")
public class MainTest {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(MainTest.class);
TestServiceImpl bean = context.getBean(TestServiceImpl.class);
bean.hello();
}
}
- Correct output and you're done.