本机环境
- win10
- idea2021.1
- jdk 1.8
- spring 5.2.x
- gradle-5.6.4
1.下载spring源码
https://github.com/spring-projects/spring-framework/tree/5.2.x
这里我选择5.2.x版本当然也可以选择其他版本
2.下载gradle
https://services.gradle.org/distributions/https://services.gradle.org/distributions/
根据代码版本下载适合的gradle版本
安装过程自行百度
3.修改build.gradle文件
解压zip文件后找到build.gradle文件进行编辑
添加如下代码,加快编译速度
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
4.执行gradle.bat
在源代码根目录cmd 然后输入gradle.bat 回车
成功后在 gradle objenesisRepackJar 回车
成功后在 gradle cglibRepackJar 回车
5.用idea打开项目等待编译
6.加入自己的模块
- build.gradle
plugins {
id 'java'
}
group 'com.zyj'
version '5.2.20.BUILD-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compile (project(":spring-context"))
compile (project(":spring-instrument"))
testImplementation 'org.projectlombok:lombok:1.18.6'
}
test {
useJUnitPlatform()
}
-
MainConfig
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.zyj")
public class MainConfig {
}
-
Car
public class Car {
}
-
IndexService
import org.springframework.stereotype.Component;
@Component
public class IndexService {
public IndexService(){
System.out.printf("hello indexService!");
}
public void getIndexService(){
System.out.printf("hello getIndexService");
}
}
-
Main
import com.zyj.config.MainConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
// context.getBean(TestBean.class);
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
7.执行main方法
打印扫描到的两个类名
问题总结
8.1 gradle编译中文注释乱码
在阅读spring源码的过程中我们肯定是要在上面添加注释的,但是当我添加了中文注释后再去编译发现出现了乱码问题。在网上找了几个解决方法后尝试成功解决!
1.Help-->Edit Custom VM Options...
2.在文档的最后加上
-Dfile.encoding=UTF-8
3.File-->Editor--->File Encodings
4.保存-----》重新编译问题解决!!!
8.2找不到符号: 类 AnnotationCacheAspect
解决步骤
1.下载AspectJ
下载地址:AspectJ Downloads | The Eclipse Foundation
选择版本后点击,然后下载即可
2.安装
注意:要确保自己配置好jdk环境
到jar包存放目录cmd 然后 java -jar aspectj-1.9.4.jar 执行
一般会默认填充正确地址,如果没有填充可手动设置
安装目录可手动更改
3.为spring-aspect 工程手动添加Facets属性
File -> Project Structure
同上面给spring-aop_main添加Facets属性
4.更改编译器
5.编译成功,问题解决