相關(guān)資源版本
idea:2021
JDK:1.8
gradle:5.6.4(具體依據(jù)spring源碼中的要求版本)
spring:5.2.x(不建議使用最新版本)
git:2.7.3(無版本要求)
系統(tǒng):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的分支,容易忘記切換分支殷蛇,然后下載的是最新代碼,不容易導(dǎo)入成功
可以進(jìn)入spring-framework目錄戒幔,然后在gradle.properties文件中查看當(dāng)前使用的spring版本,
git checkout 5.2.x
配置gradle
1土童、打開https://gradle.org/releases進(jìn)行下載诗茎,一定要下載對應(yīng)版本的gradle,才能編譯成功献汗,可以進(jìn)入spring-framework-》gradle-》wrapper目錄中敢订,查看gradle-wrapper.properties文件中g(shù)radle版本信息
2王污、配置gradle環(huán)境變量GRADLE_HOME、GRADLE_USER_HOME楚午、path,GRADLE_USER_HOME可以使用maven的倉庫
使用idea打開源碼
1昭齐、打開idea,然后選擇spring-project中的build.gradle文件醒叁,
導(dǎo)入項目后gradle使用jdk8司浪,
導(dǎo)入后,項目會構(gòu)建比較慢
2把沼、打開build.gradle文件,在文件頭部加上Spring插件庫
這個就相當(dāng)于是maven的pom文件)
repositories {
maven { url 'https://repo.spring.io/plugins-release-local' }
}
}
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饮睬、保存后等待下載依賴
都修改好之后,保存一下篮奄,點擊頁面下圖刷新圖標(biāo)捆愁,等待項目下載依賴構(gòu)建即可,如果有報錯多刷新幾遍
對源碼進(jìn)行編譯
1窟却、先編譯spring-oxm下的compileTestjava昼丑,選中雙擊即可,編譯完成出現(xiàn)BUILD SUCCESSFUL代表編譯完成
2夸赫、再編譯下spring-core模塊菩帝,因為之后的spring-context依賴于core,方法同上
3茬腿、都編譯完成且成功之后呼奢,最后開始編譯整個工程(這個過程非常耗時間),打開頂層項目spring->build->build切平,雙擊開始編譯
測試基本功能
都編譯成功之后握础,可以添加一個測試模塊進(jìn)來,進(jìn)行測試源碼構(gòu)建編譯是否真的完成
1悴品、新建測試modle禀综,步驟:【File】->【New】->【Module…】 在Spring中添加自己的module模塊,同樣選擇gradle構(gòu)建苔严。
2定枷、添加依賴
找到我們自己的測試模塊test,打開build.gradle文件(相當(dāng)于是pom文件)邦蜜,默認(rèn) dependencies依賴(這里的dependencies和maven里的依賴是一樣的)我們需要手工添 加spring-context依鸥,spring-beans,spring-core悼沈,spring-aop這4個核心模塊贱迟,具體如下
dependencies {
//添加完要構(gòu)建一下姐扮,否則代碼中無法引用,注意不要忘了
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進(jìn)行構(gòu)建,構(gòu)建完成后缚俏,才能正常使用
構(gòu)建后的項目情況截圖:
編寫測試代碼
下面編寫一個簡單的applicationContext獲取容器用的bean惊搏,主要是測試Spring源碼構(gòu)建編譯過程是否成功厕九!
1勃痴、下面編寫一個簡單的applicationContext獲取容器用的bean,主要是測試Spring源碼構(gòu)建編譯過程是否成功烤咧!
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]
出現(xiàn)以上錯誤是因為spring使用了ant風(fēng)格的代碼檢查刹缝,所有的檢查規(guī)則都在src/checkstyle/checkstyle.xml里面做了配置碗暗,如果不想被這些檢查煩擾到,可以全部注釋掉:
<!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類,結(jié)果如下:
表明源碼導(dǎo)入成功
導(dǎo)入源碼颂砸,有太多的坑噪奄,參考文章:
https://blog.csdn.net/m0_37959155/article/details/126273608
https://blog.csdn.net/qq_25825005/article/details/122518377