Spring5源码编译

2020-11-26  本文已影响0人  逍遥白亦

1. 准备工作

1.1 环境准备

1.2 源码下载

a. 源码
b. 下载最新的RELEASE版本即可

1.3 gradle

Mac版本安装
Windows版本安装

注意:一定要下载gradle5.x版本,6.x版本不支持本次编译。

2. 编译

2.1 编译compileTestJava模块

做完准备工作之后,进入spring源码目录,Windows版本执行:

gradlew :spring-oxm:compileTestJava

Mac版本执行:

./gradlew :spring-oxm:compileTestJava

2.2 导入项目到idea中

导入项目到idea中:Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)

等待编译完成。

2.3 添加测试模块代码

new->model->gradle-->输入模块名称

2.3.1 添加依赖

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

2.3.2 添加任意bean

package com.spring.beans;

import org.springframework.stereotype.Service;

import javax.xml.ws.ServiceMode;

@Service
public class UserServiceImpl {

    public void sayHi(){
        System.out.println("Hello Spring!");
    }

}

2.3.3 添加启动类

import com.spring.beans.UserServiceImpl;
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("com.spring")
public class MainStat {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MainStat.class);
        UserServiceImpl bean = context.getBean(UserServiceImpl.class);
        bean.sayHi();
    }

}

2.3.4 正确输出,大功告成

能看到正确输出"Hello Spring!"的话,编译就成功了。

上一篇 下一篇

猜你喜欢

热点阅读