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;
  }
}
相关文章
|
10月前
|
XML Java Nacos
Spring Boot 整合Nacos 版本兼容适配 史上最详细文档
本文介绍SpringBoot整合Nacos的完整流程,涵盖Nacos下载安装、配置中心与服务发现集成、版本兼容性问题及实战配置。重点解决SpringBoot 3.3.0与Nacos版本适配难题,推荐使用Spring Cloud Alibaba方案,并提供项目开源地址供参考学习。
|
9月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
2097 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
运维 Linux 虚拟化
VMware虚拟机安装教程,Windows下安装VMware虚拟机,附VMware下载,Windows各版本系统镜像下载
虚拟机技术允许一台物理机运行多个操作系统,提升资源利用率,节省成本。通过快照、克隆等功能,实现系统快速恢复与复制,提高运维效率。本文详细介绍VMware虚拟机的安装步骤、Windows镜像下载及系统安装激活流程,适合初学者快速入门。
13090 0
|
11月前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
1441 0
|
Windows
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
337 2
|
11月前
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
1760 0
|
Linux Docker Windows
windows docker安装报错适用于 Linux 的 Windows 子系统必须更新到最新版本才能继续。可通过运行 “wsl.exe --update” 进行更新。
适用于 Linux 的 Windows 子系统需更新至最新版本(如 wsl.2.4.11.0.x64.msi)以解决 2025 年 Windows 更新后可能出现的兼容性问题。用户可通过运行 “wsl.exe --update” 或访问提供的链接下载升级包进行更新。
4416 0