1.安装gradle
1.下载gradle
2.把下载好的zip包解压到安装路径
3.配置环境变量
把环境变量添加到path中
安装成功
2.下载项目
码云项目路径: https://gitee.com/mirrors/Spring-Framework/tree/5.1.x/
下载并切换分支命令: git clone https://gitee.com/mirrors/Spring-Framework.git -b 5.1.x
3.导入项目
然后选择刚clone下来的项目,选择gradle构建,然后按照如下配置gradle
调整jvm参数是因为项目较大如果不调大在编译运行的时候可能会OOM
把gradle面板搞到idea右侧
3.1 编译spring项目前的准备
1.注释 dokka和asciidoctor
4.开始编译
在spring的import-into-idea.md 文件中会有描述具体的编译步骤
4.1 编译spring-xom
在右侧面板选择对应的 compileTestJava
如果报错如下:
Could not resolve compiler classpath. Check if Kotlin Gradle plugin repository is configured in project ':spring-oxm'.
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':spring-oxm:compileTestKotlin'.
> Could not resolve all files for configuration ':spring-oxm:kotlinCompilerClasspath'.
> Could not resolve org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71.
Required by:
project :spring-oxm
> No cached version of org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71 available for offline mode.
> No cached version of org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71 available for offline mode.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
No cached version of org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71 available for offline mode.
Disable Gradle 'offline mode' and sync project
把 Offline work 的 √ 去掉
如果报错如下,错误信息中有 read time out 之类的,可能是网络不稳定,重新点击 compileTestJava
Execution failed for task ':spring-oxm:compileTestJava'.
编译成功
ok,以上其实只是预编译操作
4.2 编译spring-core
4.3 正式编译 spring-framework
双击 build
这一步操作时间较长,建议睡一觉回来在看看成功没
5.运行测试类
第一次报错
解决: 因为spring-context对spring-instrument有依赖,在spring-context 的build.gradle添加对spring-instrument的依赖即可。
第二次报错
spring-context-support添加依赖
compile(project(":spring-tx"))
compile(project(":spring-jdbc"))
运行成功
6.新增模块
添加依赖
plugins {
id 'java'
}
group 'org.springframework'
version '5.1.17.BUILD-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war' // 打war包需要
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-webmvc")) // 可选。看项目运行具体需要哪个就添加哪个
compile(project(":spring-context"))
compile(project(":spring-instrument"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
创建类
package com.xiaozhuang.context.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @version V1.0
* @author: sgh
* @date: 2020/7/16 23:14
* @description:
*/
@Configuration
@ComponentScan("com.xiaozhuang.**")
public class AppConfig {
}
package com.xiaozhuang.context.service;
public interface UserServer {
}
package com.xiaozhuang.context.service.impl;
import com.xiaozhuang.context.service.UserServer;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("singleton")
public class UserServiceImpl implements UserServer {
public UserServiceImpl() {
System.out.println(this.getClass().getSimpleName() + " 构造函数执行...");
}
}
package com.xiaozhuang.context;
import com.xiaozhuang.context.config.AppConfig;
import com.xiaozhuang.context.service.UserServer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ContextApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
AppConfig.class
);
UserServer bean = context.getBean(UserServer.class);
System.out.println(bean);
}
}
执行成功