相关资源版本
idea:2021
JDK:1.8
gradle:5.6.4(具体依据spring源码中的要求版本)
spring:5.2.x(不建议使用最新版本)
git:2.7.3(无版本要求)
系统:win10
获取源码
git clone -b 5.2.x https://github.com/spring-projects/spring-framework.git
或者使用
git clone https://github.com/spring-projects/spring-framework.git
然后切换到5.2.x的分支,容易忘记切换分支,然后下载的是最新代码,不容易导入成功
可以进入spring-framework目录,然后在gradle.properties文件中查看当前使用的spring版本,
git checkout 5.2.x
配置gradle
1、打开https://gradle.org/releases进行下载,一定要下载对应版本的gradle,才能编译成功,可以进入spring-framework-》gradle-》wrapper目录中,查看gradle-wrapper.properties文件中gradle版本信息
2、配置gradle环境变量GRADLE_HOME、GRADLE_USER_HOME、path,GRADLE_USER_HOME可以使用maven的仓库
使用idea打开源码
1、打开idea,然后 image.png选择spring-project中的build.gradle文件, image.png
导入项目后gradle使用jdk8,
image.png
导入后,项目会构建比较慢
2、打开build.gradle文件,在文件头部加上Spring插件库
这个就相当于是maven的pom文件)
repositories {
maven { url 'https://repo.spring.io/plugins-release-local' }
}
}
image.png
3、bulid.gradle文件添加阿里镜像,注意添加文件的位置及顺序
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url "https://repo.springsource.org/plugins-release" }
4、保存后等待下载依赖
都修改好之后,保存一下,点击页面下图刷新图标,等待项目下载依赖构建即可,如果有报错多刷新几遍
image.png
对源码进行编译
1、先编译spring-oxm下的compileTestjava,选中双击即可,编译完成出现BUILD SUCCESSFUL代表编译完成
image.png
2、再编译下spring-core模块,因为之后的spring-context依赖于core,方法同上
image.png
3、都编译完成且成功之后,最后开始编译整个工程(这个过程非常耗时间),打开顶层项目spring->build->build,双击开始编译
image.png
测试基本功能
都编译成功之后,可以添加一个测试模块进来,进行测试源码构建编译是否真的完成
1、新建测试modle,步骤:【File】->【New】->【Module…】 在Spring中添加自己的module模块,同样选择gradle构建。
image.png
image.png
2、添加依赖
找到我们自己的测试模块test,打开build.gradle文件(相当于是pom文件),默认 dependencies依赖(这里的dependencies和maven里的依赖是一样的)我们需要手工添 加spring-context,spring-beans,spring-core,spring-aop这4个核心模块,具体如下
dependencies {
//添加完要构建一下,否则代码中无法引用,注意不要忘了
compile(project(":spring-context"))
compile(project(":spring-beans"))
compile(project(":spring-core"))
compile(project(":spring-aop"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
3、添加完成依赖后,需要对测试modle进行构建,构建完成后,才能正常使用
image.png
构建后的项目情况截图:
image.png
编写测试代码
下面编写一个简单的applicationContext获取容器用的bean,主要是测试Spring源码构建编译过程是否成功!
1、下面编写一个简单的applicationContext获取容器用的bean,主要是测试Spring源码构建编译过程是否成功!
package com.clf;
public class User {
private String id;
private String userName;
public User() {
}
public User(String id, String userName) {
this.id = id;
this.userName = userName;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "com.clf.User{" +
"id='" + this.id + '\'' +
", userName='" + this.userName + '\'' +
'}';
}
}
2、新建JavaConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class JavaConfig {
@Bean
public User user(){
return new User("001","Jack");
}
}
3、最后编写一个测试类Test.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
System.out.println("hello");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
User user = (User) context.getBean("user");
System.out.println(user.toString());
}
}
4、运行Test类
注意: 运行时可能会报如下错误:
> Checkstyle rule violations were found. See the report at: file:///C:/chenlf/private_workspace/spring-framework/myTest/build/reports/checkstyle/main.html
Checkstyle files with violations: 3
Checkstyle violations by severity: [error:5]
出现以上错误是因为spring使用了ant风格的代码检查,所有的检查规则都在src/checkstyle/checkstyle.xml里面做了配置,如果不想被这些检查烦扰到,可以全部注释掉:
image.png
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="com.puppycrawl.tools.checkstyle.Checker">
<!-- Suppressions -->
<!-- <module name="SuppressionFilter">-->
<!-- <property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>-->
<!-- </module>-->
<!-- <!– Root Checks –>-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringHeaderCheck">-->
<!-- <property name="fileExtensions" value="java" />-->
<!-- <property name="headerType" value="apache2"/>-->
<!-- <property name="headerCopyrightPattern" value="20\d\d-20\d\d"/>-->
<!-- <property name="packageInfoHeaderType" value="none"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck">-->
<!-- <property name="lineSeparator" value="lf"/>-->
<!-- </module>-->
<!-- <!– TreeWalker Checks –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.TreeWalker">-->
<!-- <!– Annotations –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck">-->
<!-- <property name="elementStyle" value="compact" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.annotation.PackageAnnotationCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck">-->
<!-- <property name="allowSamelineSingleParameterlessAnnotation"-->
<!-- value="false" />-->
<!-- </module>-->
<!-- <!– Block Checks –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck">-->
<!-- <property name="option" value="text" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck">-->
<!-- <property name="option" value="alone" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck" />-->
<!-- <!– Class Design –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.InterfaceIsTypeCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.MutableExceptionCheck">-->
<!-- <property name="format" value="^.*Exception$" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck" />-->
<!-- <!– Type Names –>-->
<!-- <module name="TypeName">-->
<!-- <property name="format" value="^[A-Z][a-zA-Z0-9_]*(?<!com.clf.Test)$" />-->
<!-- <property name="tokens" value="CLASS_DEF" />-->
<!-- <message key="name.invalidPattern"-->
<!-- value="Class name ''{0}'' must not end with ''com.clf.Test'' (checked pattern ''{1}'')." />-->
<!-- </module>-->
<!-- <!– Coding –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanReturnCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedForDepthCheck">-->
<!-- <property name="max" value="3" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck">-->
<!-- <property name="max" value="5" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedTryDepthCheck">-->
<!-- <property name="max" value="3" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.MultipleVariableDeclarationsCheck" />-->
<!-- <module name="io.spring.javaformat.checkstyle.filter.RequiresOuterThisFilter" />-->
<!-- <module name="io.spring.javaformat.checkstyle.filter.IdentCheckFilter">-->
<!-- <property name="names" value="logger" />-->
<!-- <module-->
<!-- name="com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck">-->
<!-- <property name="checkMethods" value="false" />-->
<!-- <property name="validateOnlyOverlapping" value="false" />-->
<!-- </module>-->
<!-- </module>-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringNoThisCheck">-->
<!-- <property name="names" value="logger" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.coding.OneStatementPerLineCheck" />-->
<!-- <!– Imports –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck">-->
<!-- <property name="processJavadoc" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck">-->
<!-- <property name="groups" value="java,javax,*,org.springframework" />-->
<!-- <property name="ordered" value="true" />-->
<!-- <property name="separated" value="true" />-->
<!-- <property name="option" value="bottom" />-->
<!-- <property name="sortStaticImportsAlphabetically" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedImports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses"-->
<!-- value="^reactor\.core\.support\.Assert,^org\.slf4j\.LoggerFactory" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedJUnit3Imports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses" value="^junit\.framework\..+" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedJUnit4Imports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses"-->
<!-- value="^org\.junit\.(com.clf.Test|BeforeClass|AfterClass|Before|After|Ignore|FixMethodOrder|Rule|ClassRule|Assert|Assume)$,^org\.junit\.(Assert|Assume)\..+,^org\.junit\.(experimental|internal|matchers|rules|runner|runners|validator)\..+" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedJUnitJupiterImports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses" value="^org\.junit\.jupiter\..+" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedTestNGImports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses" value="^org\.testng\..+," />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">-->
<!-- <property name="id" value="bannedHamcrestImports"/>-->
<!-- <property name="regexp" value="true" />-->
<!-- <property name="illegalClasses" value="^org\.hamcrest\..+" />-->
<!-- </module>-->
<!-- <!– Javadoc Comments –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck">-->
<!-- <property name="scope" value="package"/>-->
<!-- <property name="authorFormat" value=".+\s.+"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck">-->
<!-- <property name="allowMissingParamTags" value="true"/>-->
<!-- <property name="allowMissingThrowsTags" value="true"/>-->
<!-- <property name="allowMissingReturnTag" value="true"/>-->
<!-- <property name="allowMissingJavadoc" value="true"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck">-->
<!-- <property name="scope" value="public"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck">-->
<!-- <property name="checkEmptyJavadoc" value="true"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagContinuationIndentationCheck">-->
<!-- <property name="offset" value="0"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.AtclauseOrderCheck">-->
<!-- <property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF"/>-->
<!-- <property name="tagOrder" value="@author, @since, @param, @see, @version, @serial, @deprecated"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.javadoc.AtclauseOrderCheck">-->
<!-- <property name="target" value="METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>-->
<!-- <property name="tagOrder" value="@param, @return, @throws, @since, @deprecated, @see"/>-->
<!-- </module>-->
<!-- <!– Miscellaneous –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck">-->
<!-- <property name="tokens" value="BLOCK_COMMENT_BEGIN"/>-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.UpperEllCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.ArrayTypeStyleCheck" />-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck" />-->
<!-- <!– Regexp –>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="format" value="^\t* +\t*\S" />-->
<!-- <property name="message"-->
<!-- value="Line has leading space characters; indentation should be performed with tabs only." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck">-->
<!-- <property name="format" value="[ \t]+$" />-->
<!-- <property name="illegalPattern" value="true" />-->
<!-- <property name="message" value="Trailing whitespace" />-->
<!-- </module>-->
<!-- <module-->
<!-- name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="maximum" value="0" />-->
<!-- <property name="format"-->
<!-- value="assertThatExceptionOfType\((NullPointerException|IllegalArgumentException|IOException|IllegalStateException)\.class\)" />-->
<!-- <property name="message"-->
<!-- value="Please use specialized AssertJ assertThat*Exception method." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="id" value="bddMockito"/>-->
<!-- <property name="maximum" value="0"/>-->
<!-- <property name="format" value="org\.mockito\.Mockito\.(when|doThrow|doAnswer)" />-->
<!-- <property name="message" value="Please use BDDMockito." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="id" value="expectedExceptionAnnotation"/>-->
<!-- <property name="maximum" value="0"/>-->
<!-- <property name="format" value="\@com.clf.Test\(expected" />-->
<!-- <property name="message" value="Please use AssertJ assertions." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="id" value="junit4Assertions"/>-->
<!-- <property name="maximum" value="0"/>-->
<!-- <property name="format" value="org\.junit\.Assert\.assert" />-->
<!-- <property name="message" value="Please use AssertJ assertions." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="id" value="junitJupiterAssertions"/>-->
<!-- <property name="maximum" value="0"/>-->
<!-- <property name="format" value="org\.junit\.jupiter\.api\.Assertions\.assert" />-->
<!-- <property name="message" value="Please use AssertJ assertions." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">-->
<!-- <property name="id" value="testNGAssertions"/>-->
<!-- <property name="maximum" value="0"/>-->
<!-- <!– should cover org.testng.Assert and org.testng.AssertJUnit –>-->
<!-- <property name="format" value="org\.testng\.Assert(JUnit)?\.assert" />-->
<!-- <property name="message" value="Please use AssertJ assertions." />-->
<!-- <property name="ignoreComments" value="true" />-->
<!-- </module>-->
<!-- <!– Spring Conventions –>-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringLambdaCheck">-->
<!-- <property name="singleArgumentParentheses" value="false" />-->
<!-- </module>-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringCatchCheck" />-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck" />-->
<!-- <module name="io.spring.javaformat.checkstyle.check.SpringJUnit5Check" />-->
<!-- </module>-->
</module>
去掉这些错误,错误就能解决,再次运行Test类,结果如下:
表明源码导入成功
导入源码,有太多的坑,参考文章:
https://blog.csdn.net/m0_37959155/article/details/126273608
https://blog.csdn.net/qq_25825005/article/details/122518377
网友评论