Mac阅读spring 5.0.x版本源码准备(windows差不多一样),附报错解决及准备阶段调试

简介: Mac阅读spring 5.0.x版本源码准备(windows差不多一样),附报错解决及准备阶段调试

以下是mac的操作方式,windows用户下载git,安装后鼠标在空格地方鼠标右击可以看到git bash,这个可以使用mac和linux一样的shell命令

建议IDEA版本位2020及以上,jdk位jdk8或者jdk11


源码地址:https://codechina.csdn.net/mirrors/spring-projects/spring-framework/-/tree/5.0.x


项目目录下有import-into-idea.md如下描述,为如何安装

The following has been tested against IntelliJ IDEA 2016.2.2
## Steps
_Within your locally cloned spring-framework working directory:_
1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
4. Code away
## Known issues
1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies.
See `*RepackJar` tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605).
2. `spring-aspects` does not compile due to references to aspect types unknown to
IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the
'spring-aspects' can be excluded from the project to avoid compilation errors.
3. While JUnit tests pass from the command line with Gradle, some may fail when run from
IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within
IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors:
    -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
4. If you invoke "Rebuild Project" in the IDE, you'll have to generate some test
resources of the `spring-oxm` module again (`./gradlew :spring-oxm:compileTestJava`)    
## Tips
In any case, please do not check in your own generated .iml, .ipr, or .iws files.
You'll notice these files are already intentionally in .gitignore. The same policy goes for eclipse metadata.
## FAQ
Q. What about IntelliJ IDEA's own [Gradle support](https://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)?
A. Keep an eye on https://youtrack.jetbrains.com/issue/IDEA-53476

下载zip包或者git clone到本地


1.png

找项目所需gradle的版本

到下载好的目录下查看所需gradle版本(Mac查询方式,windows用编辑文件查看gradleVersion关键字查看版本)

cat build.gradle | grep gradleVersion


找版本去官网下载

地址:https://gradle.org/releases/

1.png

加入gradle环境变量

vi /etc/profile 
GRADLE_HOME=/Users/dasouche/Downloads/gradle-4.4.1
export PATH=$GRADLE_HOME/bin:$PATH
source  /etc/profile 

进入下载好的spring代码路径下进行编译

gradle

需要下载额外的两个

gradle objenesisRepackJar
gradle cglibRepackJar

看到BUILD SUCCESSFUL就编译完成了

编译好的项目导入IDEA

IDEA-》File-〉Open-》选择spring源码路径下的build.gradle文件-〉选择As a project 完成导入

1.png

idea会自动下载Spring依赖,如果依赖下载慢,则在build.gradle文件中新增一个maven下载地址

repositories {
   mavenCentral()
   maven { url "https://repo.spring.io/libs-spring-framework-build" }
   maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
}

1.png

之后等待依赖下载完成进行测试(时间略长)

编译后java类中出现:import jdk.jfr.Category; 的错误

1.png

image.png

新建moudule测试

image.png

报错解决(参考如下配置):

plugins {
    id 'java'
}
group 'org.springframework'
version '5.0.21.BUILD-SNAPSHOT'
repositories {
    mavenCentral()
}
dependencies {
    compile(project(":spring-context"))
    compile(project(":spring-aop"))
    compile(project(":spring-aspects"))
    testCompile group:'junit', name:'junit', version:'4.12'
    testCompile group:'org.aspectj', name:'aspectjweaver', version:'1.8.14'
//    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
//    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
//test {
//    useJUnitPlatform()
//}

之后点击屏幕中的🐘

1.png

出现如下表示构建ok

1.png

开始测试(循环依赖、AOP)

public class SpringTest {
  public static void main(String[] args) {
  //构造入参是包地址
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("test");
    UserA bean = annotationConfigApplicationContext.getBean(UserA.class);
    UserB beanB = annotationConfigApplicationContext.getBean(UserB.class);
    System.out.println(beanB.testAdvice());
  }
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@EnableAspectJAutoProxy(exposeProxy = true)
@Component
@Aspect
public class TestAdvice {
  //配置切入点
  @Pointcut("execution(* test.UserB.testAdvice())")
  public void pointcut(){};
  @Before("pointcut()")
  public void before(){
    System.out.println("前置通知");
  }
  @After("pointcut()")
  public void after(){
    System.out.println("后置通知");
  }
  @AfterReturning(value = "pointcut()",returning = "obj")
  public void afterReturning(Object obj){
    System.out.println("正在执行返回通知...");
    System.out.println("目标方法的返回值是:"+obj);
  }
  @AfterThrowing(value = "pointcut()", throwing = "e")
  public void afterThrowing(JoinPoint joinPoint, Exception e){
    System.out.println("异常信息:"+e.getMessage());
  }
  @Around("pointcut()")
  public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    //前增强
    System.out.println("环绕正在执行前增强...");
    //调用目标方法
    Object object=proceedingJoinPoint.proceed();
    //后增强
    System.out.println("环绕正在执行后增强...");
    return object;
  }
}
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class UserB {
  private String name;
  private UserA userA;
  @PostConstruct
  private void init(){
    System.out.println("init userB");
  }
  public String testAdvice(){
    System.out.println("UserB test Advice");
    return "test";
  }
  public UserA getUserA() {
    return userA;
  }
  public void setUserA(UserA userA) {
    this.userA = userA;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class UserA {
  private String name;
  private UserB userB;
  @PostConstruct
  private void init(){
    System.out.println("init userA");
  }
  public UserB getUserB() {
    return userB;
  }
  public void setUserB(UserB userB) {
    this.userB = userB;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
相关文章
|
8月前
|
人工智能 测试技术 API
谁说 WebSocket 调试难?Windows 上的“秘密武器”都在这儿,速占先机
从 Apifox 这样集大成者,到 Websocat 这样专注于命令行极致体验的工具,再到 WebSocket King 的开源灵活,以及 Postman 的广泛适用性和 WebSocket.org Toolkit 的即时性,它们共同构成了 WebSocket 开发生态的重要支撑。
|
前端开发 Java 物联网
智慧班牌源码,采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署
智慧班牌系统是一款基于信息化与物联网技术的校园管理工具,集成电子屏显示、人脸识别及数据交互功能,实现班级信息展示、智能考勤与家校互通。系统采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署与私有化定制。核心功能涵盖信息发布、考勤管理、教务处理及数据分析,助力校园文化建设与教学优化。其综合性和可扩展性有效打破数据孤岛,提升交互体验并降低管理成本,适用于日常教学、考试管理和应急场景,为智慧校园建设提供全面解决方案。
709 70
|
9月前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
1341 0
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
626 7
|
存储 监控 数据可视化
SaaS云计算技术的智慧工地源码,基于Java+Spring Cloud框架开发
智慧工地源码基于微服务+Java+Spring Cloud +UniApp +MySql架构,利用传感器、监控摄像头、AI、大数据等技术,实现施工现场的实时监测、数据分析与智能决策。平台涵盖人员、车辆、视频监控、施工质量、设备、环境和能耗管理七大维度,提供可视化管理、智能化报警、移动智能办公及分布计算存储等功能,全面提升工地的安全性、效率和质量。
365 0
|
Java 关系型数据库 数据库连接
Spring源码解析--深入Spring事务原理
本文将带领大家领略Spring事务的风采,Spring事务是我们在日常开发中经常会遇到的,也是各种大小面试中的高频题,希望通过本文,能让大家对Spring事务有个深入的了解,无论开发还是面试,都不会让Spring事务成为拦路虎。
524 1
|
Java Spring
Spring底层架构源码解析(三)
Spring底层架构源码解析(三)
685 5
|
Java Spring 容器
Spring IOC、AOP与事务管理底层原理及源码解析
【10月更文挑战第1天】Spring框架以其强大的控制反转(IOC)和面向切面编程(AOP)功能,成为Java企业级开发中的首选框架。本文将深入探讨Spring IOC和AOP的底层原理,并通过源码解析来揭示其实现机制。同时,我们还将探讨Spring事务管理的核心原理,并给出相应的源码示例。
572 9
|
XML Java 数据格式
Spring底层架构源码解析(二)
Spring底层架构源码解析(二)
322 4

热门文章

最新文章