Spring源码搭建

环境搭建

JDK配置

笔者这边选择jdk11,具体安装过程可以看笔者这篇文章模仿【jdk环境搭建简记

Gradle配置

这边笔者选择的是gradle7.2,具体配置步骤如下:

  1. 下载gradle-7.2-bin.zip,下载地址为Gradle下载地址
  2. 在安装目录下,新增文件夹.gradle用来当做仓库,笔者这边自己选择D:\WorkAndStudy\GradleRepository该目录下文件作为仓库。
  3. 在gradle-7.2\init.d 中创建一个·init.gradle,添加以下内容
allprojects {
    repositories {
        maven { url 'file:///D:/WorkAndStudy/GradleRepository'}	//路径不要有空格
        mavenLocal()
        maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" ; allowInsecureProtocol=true}
        maven { name "Bstek" ; url "http://nexus.bsdn.org/content/groups/public/" ; allowInsecureProtocol=true }
        mavenCentral()
    }

    buildscript { 
        repositories { 
            maven { name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' ; allowInsecureProtocol=true }
            maven { name "Bstek" ; url 'http://nexus.bsdn.org/content/groups/public/' ; allowInsecureProtocol=true }
            maven { name "M2" ; url 'https://plugins.gradle.org/m2/' ; allowInsecureProtocol=true }
        }
    }
}
  1. 环境变量

添加GRADLE_HOME ,指向Gradle解压目录D:\Program Files\Programs\Java\gradle-7.2
添加GRADLE_USER_HOME,指向Gradle仓库D:\WorkAndStudy\GradleRepository
配置Path环境变量;添加%GRADLE_HOME%\bin

  1. 测试
    验证Gradle安装成功

Spring源码构建

1、获取源码

进入Spring源码下载地址,笔者选择的是5.3.17 release版本,点击下载。
也可以fork到自己的仓库然后Git clone下来。
获取源码

2、设置idea中Gradle配置

用idea打开Spring项目后,进入到File=》Settings=>搜索gradle进行配置。gradle配置

3、修改Build.gradle文件

搜索repositories

buildscript {
	repositories {
		//新增以下3个阿里云镜像
		maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
		maven { url 'https://maven.aliyun.com/repository/spring/' }
		maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
		
		mavenCentral()
		maven { url "https://repo.spring.io/libs-spring-framework-build" }
		maven { url "https://repo.spring.io/milestone" }
		maven { url "https://repo.spring.io/plugins-release" }
	}
}

4、构建

然后点击立即构建
重新构建构建成功在这里插入图片描述

5、建立测试用例验证

1、通过上面步骤,我们可以建立一个测试用例来验证该源码构建是否成 步骤:【File】->【New】->【Module】

new module

2、选择Gradle

选择gradle

3、下一步

命名

4、项目结构如下:

项目结构

5、修改build.gradle文件
plugins {
    id 'java'
}

group 'com.study'
version '5.3.17-SNAPSHOT'

repositories {
    maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
    maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    implementation(project(":spring-context"))
    implementation(project(":spring-instrument"))
}

TestService代码
package com.study.service;

/**
 * @Author lin
 * @Create 2022/5/8 19:00
 */
public interface TestService {
	public String sayHello(String name);
}

TestServiceImpl
package com.study.service.impl;

import com.study.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @Author lin
 * @Create 2022/5/8 19:01
 */
@Service(value = "TestService")
public class TestServiceImpl implements TestService {
	@Override
	public String sayHello(String name) {
		System.out.println("Last Dance,author:"+name);
		return "";
	}
}

TestConfig代码
package com.study.config;

import org.springframework.context.annotation.ComponentScan;

/**
 * @Author lin
 * @Create 2022/5/8 19:28
 */
@ComponentScan(value = "com.study.service")
public class TestConfig {
}

Main代码
package com.study;

import com.study.config.TestConfig;
import com.study.service.TestService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author lin
 * @Create 2022/5/8 19:28
 */
public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);

		TestService testService = (TestService) context.getBean("TestService");
		testService.sayHello("林北");
	}
}

执行结果

执行结果

问题解决

问题1:fatal: not a git repository (or any of the parent directories): .git
解决方案

id "io.spring.ge.conventions" version "0.0.9"注释;

问题2:Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven…
解决方案
  1. 将maven地址url由http改为https
  2. 添加关键字allowInsecureProtocol=true
maven {            
            name "xxx" ;
            url 'xxx';
            allowInsecureProtocol = true
}
问题3:出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
解决方案

在 org.springframework.core.CoroutinesUtils.invokeSuspendingFunction(Method method, Object target, Object… args) 方法上加 @SuppressWarnings(“deprecation”) 注解即可。

问题4:Kotlin: warnings found and -Werror specified
解决方案

这个问题是由于Kotlin将程序中的警告变更为错误导致的问题,只需要改变一下级别即可
问题解决方案:修改成如图所示即可,删掉Weeror
Kotlin warnings

问题5:java: 找不到符号 符号: 类 InstrumentationSavingAgent 位置: 程序包 org.spring
解决方案

在自己测试项目的build.gradle里加上 implementation(project(“:spring-instrument”))

问题6:缺少 spring-cglib-repack-xxx.jar 和 spring-objenesis-repack-xxx.jar 依赖
解决方案
  1. 在源码项目根路径下执行:gradle objenesisRepackJar、gradle cglibRepackJar。
  2. 在IntelliJ IDEA 的侧边工具打开 gradle,分别双击 spring-core -> Tasks -> other 下的objenesisRepackJar 和 cglibRepackJar。 以上两种方法均会在项目的spring-core\build\libs目录下生成所需 jar 包。
问题7:程序包jdk.jfr不存在 但是其实存在
解决方案
  1. jar下载不完全重新编译或清理缓存invalidate Caches
  2. 更改jdk版本不完全,修改 gradle 模块下的 toolchains.gradle 配置文件:
    将 JavaLanguageVersion.of(8) 改为 JavaLanguageVersion.of(11)。
    将sourceCompatibility = JavaVersion.VERSION_1_8 改为sourceCompatibility = JavaVersion.VERSION_11。
    将 jvmTarget = '1.8' 改为 jvmTarget = '11'。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值