spring项目下载

源码对应的书籍为《spring源码深度解析》,由于出书也有一段时间,为了便于对照着学习,所以准备下载的源码为3.2.x版本,下载地址为:https://github.com/spring-projects/spring-framework/tree/3.2.x。

部署

现在github上的spring项目都是由gradle部署的,所以自己电脑需要提前安装gradle工具,类似于maven,配置好环境后,可以让其更方便的执行。

下载完成后,可以看到项目根目录中有一个readme.md和import-into-idea.md两个文件。readme文档里面主要是关于一些支持的说明。import-into-idea文档告诉如何在自己的编辑器中部署运行代码。

1
2
3
4
5
6
7
8
9
## Steps

_Within your locally cloned spring-framework working directory:_

1. Generate IDEA metadata with `./gradlew :spring-oxm:compileTestJava cleanIdea idea`
2. Import into IDEA as usual
3. Set the Project JDK as appropriate
4. Add git support
5. Code away

按照要求,第一步要执行./gradlew :spring-oxm:compileTestJava cleanIdea idea。在这一步上,第一次耗时比较久,要下载一些依赖。 我在这一步遇到了这个错误

1
2
3
4
5
6
7
8
9
10
11
12
13
FAILURE: Build failed with an exception.

* Where:
Script 'E:\project2019\spring-framework-3.2.13.RELEASE\spring-oxm\oxm.gradle' line: 123

* What went wrong:
Execution failed for task ':spring-oxm:compileTestJava'.
> JiBXException in JiBX binding compilation

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

这个错误,错位的位置为oxm.gradle 123行,JiBX没有绑定的错误,结合源码,把这一段注释掉,就没有什么问题了,可以构建成功。

下一步是导入这个项目,采用gradle的形式进行导入,系统会自动下载其中的依赖,gradle 的模块关系是在根目录下面的settings.gradle中维护的,具体的模块之间的依赖在build.gradle和每个子项目自己的gradle配置文件中有定义。在这里导入会有一些问题。会报如下的错误:

1
A problem occurred configuring project ':spring-orm-hibernate4'.

百度了一下,这个问题是由于build.gradle中的merge.into = project(":spring-****")引起的,把所有的merge.into都注释了,就不会有问题了。

刷新gradle依赖后,项目导入成功,没有报错了。

运行

在spring根目录下新建一个Gradle的module,专门用来测试自己的数据,setting.gradle文件尾部会自动生成include 'mytest',自己新建的项目中有一个私有的配置build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
plugins {
id 'java'
}

group 'org.springframework'
version '3.2.13.RELEASE'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

这时,如果要使用其他模块的项目,需要在依赖中导入进来,在这个例子中,需要引入spring-beansspring-core,修改后的依赖为

1
2
3
4
5
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}

建立一个spring配置文件spring-config.xml

1
2
3
4
5
6
<?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="myTestBean" class="com.trembear.beans.MyTestBean"/>
</beans>

对应的MyTestBean为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MyTestBean {

private String name = "testName";

public MyTestBean(String name) {
this.name = name;
}

public MyTestBean() {
}

@Override
public String toString() {
return "MyTestBean{" +
"name='" + name + '\'' +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

然后写个Test类

1
2
3
4
5
6
7
8
public class AppTest {
@Test
public void test() {
BeanFactory bf = new XmlBeanFactory( new ClassPathResource("spring-config.xml"));
MyTestBean myTestBean = (MyTestBean) bf.getBean("myTestBean");
Assert.assertEquals("testName",myTestBean.getName());
}
}

运行这段代码,可以正常运行,可以按照书中提示一步步的进行源码阅读了。