以下是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到本地
找项目所需gradle的版本
到下载好的目录下查看所需gradle版本(Mac查询方式,windows用编辑文件查看gradleVersion关键字查看版本)
cat build.gradle | grep gradleVersion
找版本去官网下载
地址:https://gradle.org/releases/
加入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 完成导入
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/"} }
之后等待依赖下载完成进行测试(时间略长)
编译后java类中出现:import jdk.jfr.Category; 的错误
新建moudule测试
报错解决(参考如下配置):
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() //}
之后点击屏幕中的🐘
出现如下表示构建ok
开始测试(循环依赖、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; } }