头图

This article is the first to learn Spring source code, download Spring source code, compile, run and test.

Environmental preparation

JDK11、Gradle、Maven、SpringFramework 5.2.0.RELEASE

Download source code and compile

Into github: https://github.com/spring-projects/spring-framework

Select the desired version in Tags, and then download it on the right.

tags

After the download is complete and decompressed, enter the spring-framework-5.2.0.RELEASE file and execute the following command through the terminal:

./gradlew :spring-oxm:compileTestJava
If the download is too slow, you can use Alibaba Cloud Mirror.

执行成功

Then import the project through IDEA, and gradle will automatically compile.

The following errors may be reported during compilation:

POM relocation to an other version number is not fully supported in Gradle : xml-apis:xml-apis:2.0.2 relocated to xml-apis:xml-apis:1.0.b2.

Modify the import method, modify bulid.gradle, search for configurations.all, and add the following:

force 'xml-apis:xml-apis:1.4.01'

configurations.all {
        resolutionStrategy {
            cacheChangingModulesFor 0, "seconds"
            cacheDynamicVersionsFor 0, "seconds"
            force 'xml-apis:xml-apis:1.4.01'
        }
}

Then we exclude the spring-aspects module, right-click the module and select Load/UnLoad Modules... .

test

We create a new gradle module project springdemo for testing. The directory structure is as follows:

目录结构

Build.gradle adds dependencies. Only context is added here because core modules such as code, aop, beans, etc. have been introduced into the context.

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

First create an interface and an implementation class.

public interface WelcomeService {

    String sayHello(String name);

}
@Service
public class WelcomeServiceImpl implements WelcomeService {

    @Override
    public String sayHello(String name) {
        System.out.println("欢迎你:" + name);
        return "success";
    }
}

Create the spring configuration file, and then register the bean.

<?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="welcomeService" class="cn.jack.service.impl.WelcomeServiceImpl"/>
</beans>

Finally, we create the startup class for testing.

/**
 * @author 神秘杰克
 * 公众号: Java菜鸟程序员
 * @date 2022/3/14
 * @Description 启动类
 */
public class Entrance {

   public static void main(String[] args) {
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring-config.xml");
      WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
      welcomeService.sayHello("Spring框架!");
   }
}

operation result:

> Task :springdemo:Entrance.main()
欢迎你:Spring框架!

BUILD SUCCESSFUL in 9s

OK, here is the completion of downloading, compiling and testing the Spring source code.


神秘杰克
765 声望382 粉丝

Be a good developer.