概要
本文介绍使用IntelliJ IDEA搭建Spring Framework源代码环境,用于源代码阅读与debug。
环境搭建
1.下载源代码
访问Spring Framework在GitHub的地址,下载最新源代码。本人在下载时,版本号为5.2.2.BUILD-SNAPSHOT
https://github.com/spring-projects/spring-framework
2.根据说明导入IntelliJ IDEA
根据源代码文件中的IDEA导入说明进行操作,说明文件为源代码根目录的import-into-idea.md
Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
Code away
- 1.需要预编译spring-oxm,通过控制台,进入到Spring Framework目录,运行命令gradlew :spring-oxm:compileTestJava。
注:Spring Framework是通过Gradle进行的编译打包,故需要提前安装Gradle
- 2.导入至IDEA中, 操作方法:File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle
- 3.排除spring-aspects模块,因IDEA的问题该模块不会被识别,如不排除则会提示编译错误。
操作方法:spring-aspects右键 -> Load/Unload modules -> 添加spring-aspects为Unloaded modules
测试DEMO
在源代码项目目录下,本人直接创建了一个Maven测试项目,并创建了一个简单的Bean注册于获取的例子。
pom.xml
<?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>cn.daiwuliang</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo</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.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 引入commons-logging依赖 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://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.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.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>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
TestDemo
package demo.test;
public class TestDemo {
public String test(String str) {
return String.format("ECHO: %s", str);
}
}
App.java
package demo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
String XMLPath = "D:\\spring-framework-master\\demo\\src\\main\\java\\demo\\test\\spring-config.xml";
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(XMLPath);
TestDemo td = (TestDemo) applicationContext.getBean("testDemo");
System.out.println(td.test("Hello World!!!"));
}
}
spring-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="testDemo" class="demo.test.TestDemo"/>
</beans>
运行问题
- Demo模块需要添加相应的spring模块的依赖,添加方式:File -> Project Structure -> Dependencies,目前Demo中仅实现了简单的例子,故只添加了如下几个模块:
- spring-core中缺少commons-logging的依赖
虽然已经在demo的pom中添加了依赖,但仍旧报错,本人的解决办法为在spring-core的spring-core.gradle再添加一次,并重新构建
dependencies {
......
compile("commons-logging:commons-logging:1.2")
}
- CoroutinesRegistrar中找不到变量CoroutinesUtils
CoroutinesUtils位于spring-core目录下的kotlin-coroutines中,看结构其为一个独立的模块,并且spring-core并未关联依赖。
找到kotlin-coroutines/build/libs/kotlin-coroutines-5.2.2.BUILD-SNAPSHOT.jar->右键Add as Library
至此,运行App中的main方法,得到以下结果:
Hello World!