栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring源码搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring源码搭建

文章目录
    • 环境搭建
        • JDK配置
        • Gradle配置
    • Spring源码构建
      • 1、获取源码
      • 2、设置idea中Gradle配置
      • 3、修改Build.gradle文件
      • 4、构建
      • 5、建立测试用例验证
        • 1、通过上面步骤,我们可以建立一个测试用例来验证该源码构建是否成 步骤:【File】->【New】->【Module】
        • 2、选择Gradle
        • 3、下一步
        • 4、项目结构如下:
        • 5、修改build.gradle文件
        • TestService代码
        • TestServiceImpl
        • TestConfig代码
        • Main代码
        • 执行结果
    • 问题解决
        • 问题1:fatal: not a git repository (or any of the parent directories): .git
          • 解决方案
        • 问题2:Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven...
          • 解决方案
        • 问题3:出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
          • 解决方案
        • 问题4:Kotlin: warnings found and -Werror specified
          • 解决方案
        • 问题5:java: 找不到符号 符号: 类 InstrumentationSavingAgent 位置: 程序包 org.spring
          • 解决方案
        • 问题6:缺少 spring-cglib-repack-xxx.jar 和 spring-objenesis-repack-xxx.jar 依赖
          • 解决方案
        • 问题7:程序包jdk.jfr不存在 但是其实存在
          • 解决方案

环境搭建 JDK配置

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

Gradle配置

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

  1. 下载gradle-7.2-bin.zip,下载地址为Gradle下载地址
  2. 在安装目录下,新增文件夹.gradle用来当做仓库,笔者这边自己选择D:WorkAndStudyGradleRepository该目录下文件作为仓库。
  3. 在gradle-7.2init.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 FilesProgramsJavagradle-7.2
添加GRADLE_USER_HOME,指向Gradle仓库D:WorkAndStudyGradleRepository
配置Path环境变量;添加%GRADLE_HOME%bin

  1. 测试
Spring源码构建 1、获取源码

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

2、设置idea中Gradle配置

用idea打开Spring项目后,进入到File=》Settings=>搜索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】

2、选择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;


public interface TestService {
	public String sayHello(String name);
}

TestServiceImpl
package com.study.service.impl;

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


@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;


@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;


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

问题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-corebuildlibs目录下生成所需 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'。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号