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;
  }
}
相关文章
|
17天前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
|
2月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
155 0
|
2月前
|
Windows
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
105 2
|
2月前
|
运维 Linux 虚拟化
VMware虚拟机安装教程,Windows下安装VMware虚拟机,附VMware下载,Windows各版本系统镜像下载
虚拟机技术允许一台物理机运行多个操作系统,提升资源利用率,节省成本。通过快照、克隆等功能,实现系统快速恢复与复制,提高运维效率。本文详细介绍VMware虚拟机的安装步骤、Windows镜像下载及系统安装激活流程,适合初学者快速入门。
661 0
|
2月前
|
Linux Docker Windows
windows docker安装报错适用于 Linux 的 Windows 子系统必须更新到最新版本才能继续。可通过运行 “wsl.exe --update” 进行更新。
适用于 Linux 的 Windows 子系统需更新至最新版本(如 wsl.2.4.11.0.x64.msi)以解决 2025 年 Windows 更新后可能出现的兼容性问题。用户可通过运行 “wsl.exe --update” 或访问提供的链接下载升级包进行更新。
520 0
|
Windows
Windows下版本控制器(SVN)- 配置版本库
Windows下版本控制器(SVN)- 配置版本库
20 0
|
C++ Windows
c++ windows 获取mac地址
Windows平台下用C++代码取得机器的MAC地址并不是一件简单直接的事情。到目前为止,作者尚未发现有任何一个通用的100%的适用于所有Windows平台的方法可以稳定的取得MAC地址。而有些应用(比如MMORPG)则需要稳定的得到机器的MAC地址,解决方案往往是通过多种方法依次使用来提高成功率。
1971 0
|
4月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
620 2

热门文章

最新文章