环境准备(这一步自己百度咯)
maven
jdk8+
idea
源码地址
github :https://github.com/spring-projects/spring-framework
建议下载5.X的 RELEASE版本,因为RELEASE版本比较稳定
如果有偷懒的小伙伴,可以直接下载我已经弄好的代码,已经解决了很多编译问题。以及我的源码注释都补充在上面(不过我的已经落后了几个小版本,不过不影响,因为spring核心包基本不会有大的改动)
https://github.com/WangTingYeYe/spring_source
构建工具准备:
安装源码对应的gradle版本(也可不安装),建议使用gradleWraper中的gradle。
Gadle介绍:
Gradle是个构建系统,能够简化你的编译、打包、测试过程。熟悉Java的同学,可以把Gradle类比成Maven。
Gradle Wrapper的作用是简化Gradle本身的安装、部署。不同版本的项目可能需要不同版本的Gradle,手工部署的话比较麻烦,而且可能产生冲突,所以需要Gradle Wrapper帮你搞定这些事情。Gradle Wrapper是Gradle项目的一部分。
Gradle无需花时间去深入学习,因为我们在学习源码的过程不会过多涉及到gradle,当然有兴趣可以去学习, 相当于后起之秀,但是maven已经够优秀了,应该也没有办法替代maven。
gradleWraper在该文件中有体现,相当于远程自动下载(所以你可以下载gradle,也可以不下,因为会使用远程的统一版本):spring-framework-5.2.7.RELEASE\gradle\wrapper\gradle-wrapper.properties
所以如果你需要下载也最好下载该链接对应的gradle版本
例如我的源码中:
修改build.gradle
这个文件就相当于我们Maven的pom.xml 管理项目的依赖等信息…
设置镜像为阿里云。否则编译会因为网络问题出现各种各样的问题。科学上网也不如直接拉阿里云的镜像稳定,快
当然如果你直接拉的我的代码。这里我已经全部弄好了。这里直接跳过既可
// 找到repositories 这一栏改成下面这样既可
repositories {
maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}
开始编译
cd 根目录
./gradlew build
-----------------等待编译完成
gradlew :spring-oxm:compileTestJava
---------------等待出现SUSSESS 既可完成
编译过程中可能出现的问题
提示spring-beans项目中compileJava和compileGroovy 循环依赖
Circular dependency between the following tasks:
:spring-beans:compileGroovy
\--- :spring-beans:compileJava
\--- :spring-beans:compileKotlin
\--- :spring-beans:compileGroovy (*)
可以修改spring-beans项目中的gradle.build
// 最后三行注释
// compileGroovy.dependsOn = compileGroovy.taskDependencies.values - "compileJava"
// compileKotlin.dependsOn(compileGroovy)
// compileKotlin.classpath += files(compileGroovy.destinationDir)
// 更改为下面
def deps = compileGroovy.taskDependencies.values + compileGroovy.taskDependencies.values
compileGroovy.dependsOn = deps - "compileJava"
compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)
def deps = compileGroovy.taskDependencies.values + compileGroovy.taskDependencies.values
compileGroovy.dependsOn = deps - "compileJava"
compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)
导入导入IDEA
导入项目到idea中:Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
然后继续耐心等待build完成既可
新建一个gradle-model 用于测试
经过上面已经在源码项目中创建了测试model
需要在测试model中引入你要测试spring源码模块
例如我的测试model为例
//在这里添加依赖
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile(project(":spring-context"))
}
最后一个Test类
package com.micheal.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author wangting
* @version 1.0
* @ClassName: IndexService
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2019/5/31 0031 下午 3:30
*/
@Service
public class IndexService {
@Autowired
IndexDao indexDao;
}
package com.micheal;
import com.micheal.comfig.AppConfig;
import com.micheal.dao.IndexDao;
import com.micheal.dao.IndexService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author wangting
* @version 1.0
* @ClassName: wang
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2019/4/24 0024 下午 12:49
*/
@Configuration
@ComponentScan("com.micheal")
public class Test {
public static void main(String[] args) {
// 1、父类的构造方法 -- 创建环境
// 2、创建AnnotatedBeanDefinitionReader(简称BDR) 并委托完成下面逻辑AnnotationConfigUtils.registerAnnotationConfigProcessors
// 2.1 设置 DependencyComparator AutowireCandidateResolver
// 2.2 设置6大RootBeanDefinition对象 并注册到BeanDefinitionMap 中
// 3、创建 ClassPathBeanDefinitionScanner
//初始化beanfactory 环境
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//主要是是注册BeanDefinition(spring中类的描述 类比与java中 的Class类 描述 类)
// DefaultListableBeanFactory 中有一个 beanDefinitionMap 存储的
// 这个方法就是 根据一个Class 向容器中 注册一个bd
annotationConfigApplicationContext.register(Test.class);
// annotationConfigApplicationContext.addBeanFactoryPostProcessor( new MyBeanFactoryPostProcessor());
// 这一步才是 最核心的代码
annotationConfigApplicationContext.refresh();
IndexService bean = annotationConfigApplicationContext.getBean(IndexService.class);
bean.sayHello();
}
}