idea搭建spring源码阅读环境

1.安装gradle

1.下载gradle

https://gradle.org/releases/
在这里插入图片描述

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);
	}
}

在这里插入图片描述

执行成功

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值