1.工具准备及环境配置
1.1JDK
JDK8或以上
1.2.gradle下载及配置
可参考我的另一篇文章:https://editor.csdn.net/md/?articleId=119942562
2.spring-framework下载源码
源码下载地址: 原始仓库下载: https://github.com/spring-projects/spring-framework 国内仓库下载:https://gitee.com/mirrors/Spring-Framework/tree/release-5.1.12.RELEASE/ 开源广场下载:https://codechina.csdn.net/mirrors/spring-projects/spring-framework/-/tree/v5.3.9
我是通过开源广场下载的源码:
3.推送本地源码到远程仓库
我的代码托管用的是Gitee(基于 Git 的代码托管和研发协作平台),官方https://gitee.com/
3.1方式一:远程仓库为master分支
方式一(适用待推送代码为远程仓库master分支的场景): 1.将下载好的本地源码zip包解压到本地spring-framework-v5.3.9目录 2.打开git命令窗口,git bash here 3.新建本地仓库,命令:git init 4.把所有zip包解压后的文件添加git管理,命令:git add --all 5.提交到本地仓库,命令:git commit -m “spring-framework-v5.3.9 源码” 6.添加到远程仓库,命令:git remote add origin https://gitee.com/hbxiaobai/spring-framework.git 7.推送远程仓库,命令:git push -u origin master
3.2方式二:远程仓库非master分支
方式二(适用待推送代码为远程仓库非master分支的场景): 1.远程仓库创建spring-framework-v5.3.9分支 2.克隆远程spring-framework-v5.3.9分支到本地 3.将下载好的本地源码zip包解压到本地spring-framework-v5.3.9目录 4.把所有zip包解压后的文件添加git管理,命令:git add --all 5.提交到本地仓库,命令:git commit -m “spring-framework-v5.3.9 源代码” 6.推送远程仓库,命令:git push -u origin spring-framework-v5.3.9 注意:源码中target目录文件也要一起添加提交到git远程仓库,git add --all命令默认忽略target目录不会添加,建议手动搜索添加target目录及目录下的文件到git
4.源码导入idea
4.1.修改gradle仓库增加阿里云仓库
修改配置,增加支持阿里云仓库,在构建及依赖下载时速度更快 加入内容如下: maven { url ‘https://maven.aliyun.com/nexus/content/groups/public/’ } maven { url ‘https://maven.aliyun.com/nexus/content/repositories/jcenter’}
修改的配置文件及修改后内容如下: build.gradle
repositories {
mavenCentral()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
maven { url 'https://repo.spring.io/plugins-release' }
}
}
4.2.导入源码到idea
详细步骤在源码目录中的import-into-idea.md文件有说明 我的操作步骤如下: 文件–>新建–>从现有来源项目,选择项目build.gradle文件,等待导入完成 导入完成,如下:
5.源码编译
源码编译有多种方式, 方式一:按照import-into-idea.md中步骤1操作:预编译’ spring-oxm ’ with '。/ gradlew: spring-oxm: compileTestJava 方式二:全项目编译
我采用的是方式二,具体步骤如下: 使用idea中的Gradle插件对整个项目重新编译,如网络原因或模块间依赖关系导致编译报错,则再次编译,直至编译成功 编译完成:
6.源码测试
验证源码编译结果,在源码项目中找一个测试类,运行成功即可。 如运行spring-context中的ClassPathXmlApplicationContextTests中的任一测试方法进行测试,测试通过即可
7.构建自测模块
7.1.新建自测模块
项目单击右键–>新建–>新模块, 选择gradle,jdk1.8,java 自定义自测模块名称
自测模块创建成功,则spring-framework根目录下的settings.gradle中多一行(idea自动添加)
修改新建自测模块的gradle配置,添加依赖:
dependencies {
compile(project(":spring-context"))
compile(project(":spring-instrument"))
testCompile group: 'junit', name: 'junit', version: '4.13.2'
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
}
重新加载gradle变更内容,查看是否成功加载所需依赖,如下图:
7.2.编写自测代码
配置类:ASpringConfig.java
package com.a.mycode.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.a")
public class ASpringConfig {
}
bean类:ABean.java
package com.a.mycode.spring;
import org.springframework.stereotype.Component;
@Component
public class ABean {
private String str;
public String getStr() {
return str = "====测试bean====";
}
public void setStr(String str) {
this.str = str;
}
}
main方法测试:ABeanMain.java
package com.a.mycode.spring;
import com.a.mycode.spring.config.ASpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ABeanMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ASpringConfig.class);
ABean aBean = context.getBean(ABean.class);
System.out.println(aBean.getStr());
}
}
junit方式测试:ABeanTest.java
package com.a.mycode.spring;
import com.a.mycode.spring.config.ASpringConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ABeanTest {
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ASpringConfig.class);
ABean aBean = context.getBean(ABean.class);
System.out.println(aBean.getStr());
}
}
7.3.代码自测
Main方法自测,测试成功 Junit测试,测试成功
7.4.自测模块提交推送远程仓库
8.所遇问题及解决
8.1.问题:No tests found for given includes
详细报错信息:
> No tests found for given includes: [***Test.class](include rules) [com.a.mycode.spring.ABeanTest.test01](filter.includeTestsMatching)
原因: Gradle: IntelliJ IDEA 使用 Gradle 作为默认测试运行器,但是这个选项会把单元测试包装成 Gradle 的 Task,在命令行中运行的测试将始终在IDE中运行。 IntelliJ IDEA: 此选项将测试过程委派给 IntelliJ IDEA,在这种情况下,IntelliJ IDEA使用 JUnit 测试运行器,并且由于增量编译,测试运行得更快 Chose per test: 此选项可配置每个测试专门使用哪个测试运行器(Gradle 或 IntelliJ IDEA)
解决:设置使用 IntelliJ IDEA作为默认测试运行器
8.2.问题:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
详细报错信息:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:44)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 25 more
原因:项目gradle配置依赖junit的jar包时排除了hamcrest-core.jar的依赖,因而模块使用junit测试时无法加载hamcrest-core.jar的依赖
解决: 在自测模块的gradle配置文件,增加hamcrest-core.jar的依赖
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
|