本文将粗略的搭建一个Spring源码的阅读环境,为后面的源码阅读作一个准备。作任何事情无论是有一个完美的或者是不太完美的开头,只要去作了,那么就是一种胜利。java
因为spring使用了gradle构建工具,接下来先安装gradle。git
从Gradle官网下载gradle安装包,打开https://gradle.org/releases/
github
将下载的安装包gradle-x.x.x-all.zip解压到当前目录
spring
环境变量配置api
配置GRADLE_HOME
app
配置Pathide
打开目录行工具,输入gradle -v
,能看到gradle的版本信息表示安装已经成功
工具
Spring在github上的仓库地址是:https://github.com/spring-projects/spring-framework,本文不会直接去github上去下载源码,网速实在太慢。本文使用的码云上Spring仓库的镜像,该镜像每日同步一次,地址是https://gitee.com/mirrors/Spring-Framework测试
从git导入项目
填写要克隆的git仓库信息,能够点击右边的【Test】按钮测试,等待仓库克隆完成
打开导入的Spring项目
.gradle
目录,对于Administrator
用户,对应的目录是C:\Users\Administrator\.gradle
。该目录占用的空间通常比较多,因此在这里将这个目录放到其余的盘中。构建完成后报错以下(只列出了一部分):
... Error:(63, 30) java: cannot find symbol symbol: class Signature location: class org.springframework.cglib.core.KeyFactory ... location: class org.springframework.cglib.proxy.Enhancer Error:(152, 30) java: cannot find symbol ...
spring未了避免与cglib和objenesis冲突,将cglib和objenesis相关的包从新repack到org.springframework.cglib
和org.springframework.objenesis
包中,这部分的代码没有包含到源码当中。构建以前,能够经过添加Gradle任务来解决,见:https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md#known-issues和https://youtrack.jetbrains.com/issue/IDEA-160605
解决办法以下:
为了方便编写测试spring的代码,在spring-framework单独新建一个模块my-test
右键spring-framework项目->New->Module...
输入ArtifactId: my-test
一路下一步,最后点击完成,新的模块就建好了
添加依赖:api(project(":spring-context"))
为了能让my-test自动导入相关的依赖,在Gradle面板中右键spring节点
在my-test模块中编写程序测试
建立MyApplication
package com.zfx; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Hello hello = (Hello)ac.getBean("hello"); hello.sayHello(); } }
在resources目录下新建applicationContext.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="hello" class="com.zfx.Hello"></bean> </beans>
新建Hello
类
package com.zfx; public class Hello { public void sayHello() { System.out.println("Hello, zhangfengxian"); } }
运行MyApplication
,能够看到控制台输出:Hello, zhangfengxian
至此整个环境算是搭建好了
spring-aspects
模块构建时报错解决办法一:排除spring-aspects
模块
在工具栏点击File -> Project Structure...
解决办法二:使用Acj编译
在工具栏点击File -> Settings...
此问题的相关连接: