由于经常使用spring,并且之前有看过《spring技术内幕》和《spring源码解析》书,决定自己搭建一个spring的debug环境,下面主要说一下搭建过程和遇到问题如何解决。
可以直接去克隆我的到本地打开:https://github.com/BeatBoxerLrd/Springframework.git
1.由于spring-framework框架是由gradle搭建的,所以首先需要本地安装grandle,并进行配置
安装配置gradle请查看链接: https://blog.csdn.net/Beat_Boxer/article/details/86705682
2.要学习源码,首先要去下载源码,确保本地安装git的前提下,直接克隆
git clone https://github.com/spring-projects/spring-framework
Spring 4.2的注释版:
git clone https://github.com/wanwanpp/spring-framework-4.2.0
3.下载完成后进入该目录,如下,会发现有导入IDEA的md文件,打开
4.用文本编辑器打开红框标注的md文件
_Within your locally cloned spring-framework working directory:_
1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
4. Code away
①:需要预编译spring-oxm,进入到springframework根目录,打开cmd命令框,执行 ./gradlew :spring-oxm:compileTestJava命令,时间可能比较长,编译完成,如果报错无法编译,可以先进行②操作导入IDEA在进行使用以下操作,然后进行 ※※※操作,教程在下面,在IDEA中编译该模块如下
②:进入文中的第二步,导入工作,(File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
注意至少需要jdk1.8注意以下几个点
③:在Project Structure中需要将spring-aspects这个module除去,因为build报错。
右键spring-aspects模块,点击Load/Unload modules
选中spring-aspects点击unload之后如右图所示
※※※:如果第一步无法执行点击该操作进行执行,在IDEA中编译spring-oxm模块
5.点击bulid-buildproject,此过程可能时间比较长
6.如果第5步报以下错,可以按操作第7步,在重新build
Error:Kotlin: [Internal Error] java.lang.AbstractMethodError: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar.registerProjectComponents(Lcom/intellij/mock/MockProject;Lorg/jetbrains/kotlin/config/CompilerConfiguration;)V
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.(KotlinCoreEnvironment.kt:173)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.(KotlinCoreEnvironment.kt:114)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:401)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createCoreEnvironment(K2JVMCompiler.kt:282)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createEnvironmentWithScriptingSupport(K2JVMCompiler.kt:272)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:151)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:61)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:107)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$$inlined$ifAlive$lambda$1.invoke(CompileServiceImpl.kt:380)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$$inlined$ifAlive$lambda$1.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:892)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:919)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:891)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:378)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
7.报以上错误需要更新Kotlin插件,需要更新
File->Settings->Plugins->Kotlin,点击Update就可以了。
有时候下载失败,需要科学上网,科学上网,科学上网,科学上网配置
8.本地进行测试,然后debug模式查看没一步的调用
①.首先右击spring-framework,然后new-module,新建一个gradle模块,如右图,
②:首先在该项目下新建一个Module
③.修改pom依赖如下
junit
junit
4.12
test
commons-logging
commons-logging
1.2
④.点击Project Settings ,然后选择刚新建的模块,点击加号,添加下面四个依赖,spring-beans_main,supring-context_main,spring-core_main,spring-expression_main;如下图
⑤.项目结构
⑥.编写Ibook接口
public interface Ibook {
String buyBook(String name,String book);
}
⑦.实现BookImpl
@Component
public class BookImpl implements Ibook {
@Override
public String buyBook(String name, String book) {
System.out.println(name+"买了一本"+book);
return "success";
}
}
⑧.主类
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("SpringTest/src/main/resources/spring.xml");
Ibook book = (Ibook) applicationContext.getBean("book");
book.buyBook("Jean","《spring技术内幕》");
}
}
⑨.spring.xml文件
⑩.点击运行即可,上面输出的是当前路径,为了找配置文件的相对位置
最后,可以去github克隆我编译好的,直接本地用IDEA打开!!! 地址:https://github.com/BeatBoxerLrd/Springframework.git
祝贺你学习源码顺利!!!