IDEA读Spring源码环境搭建

转载自 原文地址

I. Spring介绍

还有什么好介绍的,做 Java 的都知道。至于为什么读源码,还有什么好说的,还不是被逼的!

II. Gradle安装

下载:https://gradle.org/install/

Windows平台下,需要配置gradle的环境变量。

  • 新增 GRADLE_HOME 环境变量,指向Gradle解压目录

  • 配置Path环境变量:新增 %GRADLE_HOME%\bin

Gradle环境搭建

III. 下载Spring源码

可以利用git下载源码或者下载源码包。

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。

idea导入源码

点击Finish之前,可以修改一些默认的配置。

修改设置

等待IDEA构建项目完成即可。ps:等的有点久….

IDEA导入成功

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。

spring源码工程结构

File–>New–>Module…–>Gradle–>Java–>Next会出现如下界面,填写module名后点击next后点击Finish即可。

新建module

创建完成后,在 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>

   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值