Spring源码学习环境搭建
本文主要记录Spring源码的阅读环境的搭建和验证
1.前置条件(我的环境)
- Intellij IDEA 2020.1.3
- jdk 1.8
- mavne3.6.1
- gradle 5.6
- git
- 操作系统Win10
2. 安装Gradle
- 下载Gradle并解压到指定目录
 官方网站
- 配置环境变量
 1.变量名:GRADLE_HOMEGradle的解压目录
- 变量名:GRADLE_USER_HOME自定义Gradle仓库目录或者Maven的仓库目录
- 添加Path %GRADLE_HOME%\bin
- 验证gradle -v 
- 配置Gradle仓库源 
 在Gradle安装目录下的 init.d 文件夹下,新建一个 init.gradle 文件,里面填写以下配置,使用阿里云仓库镜像地址- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17- allprojects { 
 repositories {
 maven { url 'file:///C:/MyProgram/development/maven/repository'}
 mavenLocal()
 maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
 maven { name "Bstek" ; url "http://nexus.bsdn.org/content/groups/public/" }
 mavenCentral()
 }
 buildscript {
 repositories {
 maven { name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' }
 maven { name "Bstek" ; url 'http://nexus.bsdn.org/content/groups/public/' }
 maven { name "M2" ; url 'https://plugins.gradle.org/m2/' }
 }
 }
 }- repositories 中写的是获取 jar 包的顺序。先是本地的 Maven 仓库路径;接着的 mavenLocal() 是获取 Maven 本地仓库的路径,应该是和第一条一样,但是不冲突;第三条和第四条是从国内和国外的网络上仓库获取;最后的 mavenCentral() 是从Apache提供的中央仓库获取 jar 包。 
- IDEA中配置Gradle 
 新版本的IDEA中设置Gradle界面有很大的变化,更加简洁。如下图 
- 配置gradle的仓库地址
- 配置idea使用我们本地安装的gradle(指定安装目录)
- 配置jdk版本
拉取源码
github 地址
码云镜像地址
github服务器地址比较慢可以使用码云的镜像地址
| 1 | git pull https://github.com/spring-projects/spring-framework | 
拉取成功后切换到指定分支
| 1 | git beanch 查看当前分支 | 
构建
- 前置命令
 在README中找到Build from Source 打开后可以看到具体的构建步骤
 由于我这里是最终通过IDEA进行构建与编码,所以选择下面的通过
 IntelliJ IDEA 构建
 可以看到大致步骤如下:过程中可能出现如下错误1 
 2
 3
 4Precompile 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 
 通过错误提示进入到指定目录删除对用目录的缓存文件,让gradle重新下载jar包  导入到IDEA中在IDEA中选择open或import进行导入,导入之后,idea会自动识别build.gradle文件并进行构建,等待一会,出现build success 代表构建成功  排除aspect模块这里说下为什么要排除,因为sapect自己有编译器,会和jvm冲突,所以先排除 
 右键spring-aspect模块 选择Load/Unload Modules 将sapect模块移除 测试& Code away
- idea中新建模块   
- 创建bean - 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
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36- /** 
 * @author haopeng
 * @date 2020-07-14 22:08
 */
 public class Person {
 /**
 * name
 */
 private String name;
 /**
 * age
 */
 private Integer age;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public Integer getAge() {
 return age;
 }
 public void setAge(Integer age) {
 this.age = age;
 }
 public String say() {
 return "name:" + name + "=== age" + age;
 }
 }
- 创建beans配置类1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 @Configuration
 public class MyConfiguration {
 @Bean
 public Person person() {
 Person person = new Person();
 person.setName("Mcgrady");
 person.setAge(28);
 return person;
 }
 }
- 测试1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 /**
 * @author haopeng
 * @date 2020-07-14 22:06
 */
 public class HellloSpring {
 public static void main(String[] args) {
 
 AnnotationConfigApplicationContext context =
 new AnnotationConfigApplicationContext(MyConfiguration.class);
 
 Person person = (Person)context.getBean("person");
 System.out.println(person.say());
 
 }
 } 
- 也可以使用xml进行bean配置1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 <?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="person" class="com.hp.beans.Person">
 <property name="name" value="haopeng"></property>
 <property name="age" value="28"></property>
 </bean>
 </beans>
 //ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 //Person person= (Person) context.getBean("person");
总结下来:Spring的源码构建中很遇到好多环境问题的坑,一定要耐心慢慢去查看错误提示,查看官网文档,或百度/google查找别人的解决办法,最终构建成功