转载自 原文地址
I. Spring介绍
还有什么好介绍的,做 Java 的都知道。至于为什么读源码,还有什么好说的,还不是被逼的!
II. Gradle安装
下载:https://gradle.org/install/
Windows平台下,需要配置gradle的环境变量。
-
新增
GRADLE_HOME
环境变量,指向Gradle解压目录 -
配置Path环境变量:新增
%GRADLE_HOME%\bin
III. 下载Spring源码
可以利用git下载源码或者下载源码包。
- github: https://github.com/spring-projects/spring-framework
- 找了一个4.2的注释版: https://github.com/wanwanpp/spring-framework-4.2.0
- 源码包:https://github.com/spring-projects/spring-framework/releases
IV. Spring源码编译
进入 spring-framework
文件夹下,打开cmd,输入 gradlew :spring-oxm:compileTestJava
进行编译。
中间可能会各种报错,可能都是网络原因,多重试几次就好了。至于需不需要科学上网,反正我是校园网没有翻墙报错多试几次直接最后就成功了。
V. IDEA导入源码
打开IDEA,File->New->Project From Existing Sources…,选中spring-framework源码文件夹,点击OK,选择Import project from external model,选中Gradle,点击Next。
点击Finish之前,可以修改一些默认的配置。
等待IDEA构建项目完成即可。ps:等的有点久….
VI. 新建测试Module
对于IDEA的project和module概念,在此就不赘述了。在此贴个链接:戳戳戳
在Project Structure中需要将spring-aspectj这个module除去,因为build报错。我在build的时候还有context下的money啥的也报错,同样exclude掉了。
spring-aspects does not compile due to references to aspect types unknown to IntelliJ IDEA. See http://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the ‘spring-aspects’ can be excluded from the project to avoid compilation errors.
为了方便测试spring源码,这里我们也在spring-framework下单独再创建一个module,专门用于编写我们自己的代码。已有的工程结构是名为spring的project下包含各种module,如图所示。所以我们新建的module也选在spring的project下,如图中的 :mytest
的module。
File–>New–>Module…–>Gradle–>Java–>Next会出现如下界面,填写module名后点击next后点击Finish即可。
创建完成后,在 mytest
的gradle配置文件中添加 compile(project(":spring-beans"))
依赖,便于下面进行容器 BeanFactory
的使用。
点击build project可能会遇到的错误:
Error:Kotlin: [Internal Error] java.lang.IllegalStateException: The provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar is not compatible with this version of compiler
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:181)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:117)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:413)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createCoreEnvironment(K2JVMCompiler.kt:276)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:63)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:108)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:52)
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:389)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$$inlined$ifAlive$lambda$1.invoke(CompileServiceImpl.kt:97)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:909)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:97)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:939)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:908)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:387)
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)
Caused by: 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.<init>(KotlinCoreEnvironment.kt:179)
... 33 more
这个主要是Kotlin插件的版本问题,解决方案是更新新版本的Kotlin插件。贴个链接:IntelliJ IDEA手动更新Kotlin.
VII. 添加日志输出
Spring5使用了log4j2,想要输出日志信息,我们需要在自己定义的模块下配置配置文件。在 src/main/resources 文件夹下新建 log4j2.xml 文件,在 src/test/resources 目录下创建 log4j2-test.xml 文件。文件配置内容为:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="DEBUG" />
<Root level="DEBUG">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>