spring source code debugging environment construction

1. Get the spring framework source code

1.1 clone mode

git clone https://github.com/spring-projects/spring-framework.git
cd spring-framework
git checkout 5.2.x # Switch to the branch you need

1.2 download mode

Access the github address of spring( https://github.com/spring-projects/spring-framework )Then switch to the branch you need to download the source package.

As shown below:

2. Spring source code structure

After obtaining the spring source code, import it into the IDEA. The code structure is as follows:

3. build

Next, build the code. On the right side of the IDEA, you will see a Gradle toolbar. Click the meeting to see the following contents. Find the command shown in the red box below and click to build. If a module fails to be built during the construction process, you can build the module separately, and then build the whole module after it is successful.

If you see the status in the figure below, it means that the construction is successful.

4. Add custom debugging module

4.1 new modules

Follow the steps shown in the screenshot below to quickly create a basic gradle module.

4.2 modify build Gradle file

After the module is created, there is only one build Gradle file, you can add modules to be debugged, as follows:

plugins {
    id 'java'
}

group 'org.springframework'
version '5.2.20.BUILD-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    // =========Add debug module start===========
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    compile(project(":spring-context"))
    compile(project(":spring-webmvc"))
    compile(project(":spring-jdbc"))
    compile(project(":spring-orm"))
    compile(project(":spring-tx"))
    compile(project(":spring-web"))
    // =========Add debug module end===========
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

4.3 add code

Then add the code to build the structure as shown in the following figure:

MyAppContext code is as follows:

package com.hamajiao.spring.debug;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyAppContext {

	public static void main(String[] args) {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

	}
}

applicationContext. The XML content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 					      					http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

4.4 debug

After completing the above steps, you can actually practice happily. debug in the following way. Let's have a look at spring first!

5. Common errors

5.1 Checkstyle error

The error contents are as follows:

Execution failed for task ':spring-debug:checkstyleMain'.
> Checkstyle rule violations were found. See the report at: file:///Users/gaoxiang/git/spring-framework/spring-debug/build/reports/checkstyle/main.html
  Checkstyle files with violations: 1
  Checkstyle violations by severity: [error:2]

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.


> Task :spring-debug:checkstyleMain FAILED
[ant:checkstyle] [ERROR] /Users/gaoxiang/git/spring-framework/spring-debug/src/main/java/com/hamajiao/spring/debug/MyAppContext.java:1: header.missing [SpringHeader]
[ant:checkstyle] [ERROR] /Users/gaoxiang/git/spring-framework/spring-debug/src/main/java/com/hamajiao/spring/debug/MyAppContext.java:5:1: Tools should be hidden public Constructor. [HideUtilityClassConstructor]

Execution failed for task ':spring-debug:checkstyleMain'.
> Checkstyle rule violations were found. See the report at: file:///Users/gaoxiang/git/spring-framework/spring-debug/build/reports/checkstyle/main.html
  Checkstyle files with violations: 1
  Checkstyle violations by severity: [error:2]

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

The above error occurs because spring uses ant style code checking, and all check rules are in Src / checkstyle / checkstyle XML is configured. If you don't want to be bothered by these checks, you can comment them all:

<?xml version="1.0"?>
<!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">-->
<!--		&lt;!&ndash; Annotations &ndash;&gt;-->
<!--		<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>-->

<!--		&lt;!&ndash; Block Checks &ndash;&gt;-->
<!--		<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" />-->

<!--		&lt;!&ndash; Class Design &ndash;&gt;-->
<!--		<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck" />-->
<!--		<module name="com.puppycrawl.tools.checkstyle.checks.design.InterfaceIsTypeCheck" />-->
<!--&lt;!&ndash;		<module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />&ndash;&gt;-->
<!--		<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" />-->

<!--		&lt;!&ndash; Type Names &ndash;&gt;-->
<!--		<module name="TypeName">-->
<!--			<property name="format" value="^[A-Z][a-zA-Z0-9_]*(?&lt;!Test)$" />-->
<!--			<property name="tokens" value="CLASS_DEF" />-->
<!--			<message key="name.invalidPattern"-->
<!--				value="Class name ''{0}'' must not end with ''Test'' (checked pattern ''{1}'')." />-->
<!--		</module>-->

<!--		&lt;!&ndash; Coding &ndash;&gt;-->
<!--		<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" />-->

<!--		&lt;!&ndash; Imports &ndash;&gt;-->
<!--		<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\.(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>-->

<!--		&lt;!&ndash; Javadoc Comments &ndash;&gt;-->
<!--		<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>-->

<!--		&lt;!&ndash; Miscellaneous &ndash;&gt;-->
<!--		<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" />-->

<!--		&lt;!&ndash; Regexp &ndash;&gt;-->
<!--		<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="\@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"/>-->
<!--			&lt;!&ndash; should cover org.testng.Assert and org.testng.AssertJUnit &ndash;&gt;-->
<!--			<property name="format" value="org\.testng\.Assert(JUnit)?\.assert" />-->
<!--			<property name="message" value="Please use AssertJ assertions." />-->
<!--			<property name="ignoreComments" value="true" />-->
<!--		</module>-->

<!--		&lt;!&ndash; Spring Conventions &ndash;&gt;-->
<!--		<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>

Of course, if you don't want to comment all the errors so roughly, you can only comment out the inspection rules corresponding to the errors. For example, I can comment out the following two rules here:

<!-- Root Checks -->
<!-- inspect SpringHeader The beginning of each class must be a copyright notice-->
<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>
<!-- inspect HideUtilityClassConstructor The utility class should privatize the constructor and declare it as final Avoid creating objects-->
<module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />

In addition, you can also modify one by one according to the rules to meet the requirements of the rules, such as changing MyAppContext to the following:

/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamajiao.spring.debug;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class MyAppContext {

	private MyAppContext() {
	}

	public static void main(String[] args) {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

	}
}

In short, the above three methods can solve this kind of error. Of course, this kind of error will not affect your debug if it is not solved, but as a late obsessive-compulsive disorder, I can't bear to see any error!

5.2 build failure of a module

When a module fails to build, you can build the module separately, and then build it as a whole after success.

5.3 connection problems

Sometimes network instability will lead to dependent download failure. Generally, you can download successfully by trying several times.

Keywords: Java Spring intellij-idea

Added by dnoonan on Sat, 15 Jan 2022 23:06:15 +0200