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;
  }
}
相关文章
|
3天前
|
Windows
如何查看自己电脑的windows系统版本?
这篇文章提供了一种简单快捷的方法来查看自己电脑的Windows系统版本,通过使用Windows的"运行"功能并输入`winver`命令来快速获取系统版本信息。
如何查看自己电脑的windows系统版本?
|
3天前
|
人工智能 前端开发 Java
【实操】Spring Cloud Alibaba AI,阿里AI这不得玩一下(含前后端源码)
本文介绍了如何使用 **Spring Cloud Alibaba AI** 构建基于 Spring Boot 和 uni-app 的聊天机器人应用。主要内容包括:Spring Cloud Alibaba AI 的概念与功能,使用前的准备工作(如 JDK 17+、Spring Boot 3.0+ 及通义 API-KEY),详细实操步骤(涵盖前后端开发工具、组件选择、功能分析及关键代码示例)。最终展示了如何成功实现具备基本聊天功能的 AI 应用,帮助读者快速搭建智能聊天系统并探索更多高级功能。
29 2
【实操】Spring Cloud Alibaba AI,阿里AI这不得玩一下(含前后端源码)
|
3天前
|
Ubuntu Linux 虚拟化
安装Windows Linux 子系统的方法:适用于windows 11 版本
本文提供了在Windows 11系统上安装Linux子系统(WSL)的详细步骤,包括启用子系统和虚拟化功能、从Microsoft Store安装Linux发行版、设置WSL默认版本、安装WSL2补丁,以及完成Ubuntu的首次安装设置。
16 2
|
22天前
|
存储 数据可视化 Python
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
67 11
|
9天前
|
设计模式 Java 程序员
学习 Spring 源码的意义是什么呢?
研究Spring源码能深化框架理解,提升代码分析与设计能力,助您掌握设计模式及最佳实践,增强解决问题的效率,促进职业生涯发展,并激发技术热情。选择稳定版本,从核心模块开始,结合实际项目并参与社区,让学习之旅既充实又具乐趣。
|
9天前
|
JavaScript 前端开发 Shell
mac和windows上安装nvm管理node版本
NVM(Node Version Manager)是前端开发者常用的命令行工具,用于管理计算机上的不同Node.js版本。通过NVM,开发者可以轻松地在多个项目间切换所需的Node.js版本。在Mac上,可以通过cURL或Wget下载安装脚本,或使用包管理工具brew安装。安装后需配置环境变量以识别NVM命令。Windows用户则可通过专用的nvm-windows安装程序完成安装。常用命令包括安装、卸载特定版本、列出已安装版本等。
15 0
|
10天前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
2月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
2月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
93 0
下一篇
云函数