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;
  }
}
相关文章
|
9天前
|
消息中间件 Java Kafka
【Azure Kafka】使用Spring Cloud Stream Binder Kafka 发送并接收 Event Hub 消息及解决并发报错
reactor.core.publisher.Sinks$EmissionException: Spec. Rule 1.3 - onSubscribe, onNext, onError and onComplete signaled to a Subscriber MUST be signaled serially.
|
4月前
|
XML Java 应用服务中间件
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
337 2
|
4月前
|
Java 关系型数据库 开发工具
idea创建不了spring2.X版本,无法使用JDK8,最低支持JDK17 , 如何用idea创建spring2.X版本,使用JDK8解决方案
本文提供了解决方案,如何在IDEA中创建Spring 2.X版本的项目并使用JDK8,尽管Spring 2.X已停止维护且IDEA不再直接支持,通过修改pom.xml或使用阿里云的国内源来创建项目。
239 0
idea创建不了spring2.X版本,无法使用JDK8,最低支持JDK17 , 如何用idea创建spring2.X版本,使用JDK8解决方案
|
5月前
|
安全 Java 应用服务中间件
Windows版本的Tomcat无法启动,如何处理?
Windows版本的Tomcat无法启动,如何处理?
440 14
|
5月前
|
Java 应用服务中间件 Spring
IDEA 工具 启动 spring boot 的 main 方法报错。已解决
IDEA 工具 启动 spring boot 的 main 方法报错。已解决
115 4
|
5月前
|
前端开发 Java Spring
【非降版本解决】高版本Spring boot Swagger 报错解决方案
【非降版本解决】高版本Spring boot Swagger 报错解决方案
264 2
|
4月前
|
并行计算 开发工具 异构计算
在Windows平台使用源码编译和安装PyTorch3D指定版本
【10月更文挑战第6天】在 Windows 平台上,编译和安装指定版本的 PyTorch3D 需要先安装 Python、Visual Studio Build Tools 和 CUDA(如有需要),然后通过 Git 获取源码。建议创建虚拟环境以隔离依赖,并使用 `pip` 安装所需库。最后,在源码目录下运行 `python setup.py install` 进行编译和安装。完成后即可在 Python 中导入 PyTorch3D 使用。
507 0
|
5月前
|
存储 Shell 开发工具
8-8|windows上Git报错
8-8|windows上Git报错
|
5月前
|
Windows
【收藏】每个Windows XP版本的缩写
【收藏】每个Windows XP版本的缩写
|
6天前
|
Java 数据库 开发者
详细介绍SpringBoot启动流程及配置类解析原理
通过对 Spring Boot 启动流程及配置类解析原理的深入分析,我们可以看到 Spring Boot 在启动时的灵活性和可扩展性。理解这些机制不仅有助于开发者更好地使用 Spring Boot 进行应用开发,还能够在面对问题时,迅速定位和解决问题。希望本文能为您在 Spring Boot 开发过程中提供有效的指导和帮助。
43 12