准备工作

  1.首先从github或者gitee上下载5.2.X分支的代码。master分支需要JDK11的支持,这里选取5.2.X只需要JDK8的支持。

  git clone -b 5.2.x  https://github.com/spring-projects/spring-framework.git

  git clone -b 5.2.x  https://gitee.com/mirrors/Spring-Framework.git

  2.环境与工具

  Win10

  JDK8, Maven3, Gradle5.6.4, IDEA2020.1

  3.以上环境配置好之后,需要改造下载的Spring源码的配置文件:

  3.1 build.gradle中,将有问题的引用注释掉:

plugins {
  id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false
  // id 'io.spring.gradle-enterprise-conventions' version '0.0.2'
  id 'io.spring.nohttp' version '0.0.5.RELEASE'
  id 'org.jetbrains.kotlin.jvm' version '1.3.72' apply false
  id 'org.jetbrains.dokka' version '0.10.1' apply false
  id 'org.asciidoctor.jvm.convert' version '2.4.0'
  id 'org.asciidoctor.jvm.pdf' version '2.4.0'
  id 'de.undercouch.download' version '4.1.1'
  id "io.freefair.aspectj" version '4.1.6' apply false
  id "com.github.ben-manes.versions" version '0.28.0'
  id 'com.gradle.build-scan' version '3.2'
  id "com.jfrog.artifactory" version '4.12.0' apply false
}

  另外,添加几个依赖,避免编译时的错误 

            dependency "com.sun.activation:javax.activation:1.2.0"
            dependency "com.sun.mail:javax.mail:1.6.2"

//添加以下三组依赖 dependency
"com.google.guava:guava:18.0" dependency "com.sun.istack:istack-commons-runtime:2.16" dependency "com.sun.xml.fastinfoset:FastInfoset:1.2.12" dependencySet(group: 'com.sun.xml.bind', version: '2.3.0.1') { entry 'jaxb-core' entry 'jaxb-impl' entry 'jaxb-xjc' }

  3.2 settings.gradle中修改maven.url为阿里云镜像,加快资源请求速度:

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
    }
}

  3.3 spring-beans/spring-beans.gradle中将最下面的四行注释掉

//def deps = compileGroovy.taskDependencies.immutableValues + compileGroovy.taskDependencies.mutableValues
//compileGroovy.dependsOn = deps - "compileJava"
//compileKotlin.dependsOn(compileGroovy)
//compileKotlin.classpath += files(compileGroovy.destinationDir)

  

  后续的步骤参考了https://www.programmersought.com/article/34134242929/

  4.参考源代码工程中的import-into-idea.md,但执行完这里的步骤还是不能顺利完成整个环境的搭建。

  4.1 在CMD中使用gradle命令进行预编译,进入到工程目录,依次执行以下两个命令:

    gradlew.bat :spring-oxm:compileTestJava

    gradlew.bat build -x test

  4.2 以上两个命令执行成功后,将代码引入IDEA中

  File -> New -> Project from Existing Sources -> Navigate to directory。引入代码后,Gradle会自动进行构建。      

  

 

 

 

      另外,在编译过程中出现了IDEA控制台中文乱码,除了在IDEA/bin目录下修改了idea.exe.vmoptions以及idea64.exe.vmoptions,在最下方添加了

-Dfile.encoding=utf-8

  在Help->Edit Custom VM Options中也添加一行

-Dfile.encoding=utf-8

  

 

 

   5. 构建工作完成之后,需要将spring-aspects排除。这是Spring's AOP内嵌的aspects,但无法在IDEA中编译通过。参考http://youtrack.jetbrains.com/issue/IDEA-64446

   5.1 选择spring-aspects,右键弹出"Load/Unload Moudules" ,将整个spring-aspects卸载

  

  6.创建工程,写一个样例代码,并将我们编译的Spring模块引入到该工程中。

   6.1

   6.1.1 新建一个Maven工程:

  

 

 

   6.1.2 引用编译的模块:

  

  6.2 修改pom文件(注意Junit已修改为4.12)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SpringFrameworkDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<name>SpringFrameworkDemo</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Introduce commons-logging dependency -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
<!-- Static resource path configuration -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

  6.3 定义了接口与实现:

  

 

 

   6.4 config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean id="loginService" class="org.example.serviceImpl.LoginImpl"/>
</beans>

  6.5 代码测试:

  

 

 

   这里已经对源代码进行了修改:

  

 

posted on 2020-12-30 23:13  长江同学  阅读(1392)  评论(0编辑  收藏  举报