一.版本说明
spring版本:5.0.X
gradle版本:4.10.1
gradle大于等于spring要求版本,具体在gradle/wrapper/gradle-wrapper.properties 这个目录下查看
步骤
1)获取源码
克隆
git clone git@github.com:cracker13/spring-framework.git
这里是我从spring官方GitHub上fork的地址,以后给源码写注释就可以传到自己的仓库上看了。当然你要是没这需求直接从官方上拉取
git clone https://github.com/spring-projects/spring-framework.git
获取历史提交记录
git log v5.0.20.RELEASE
得到sha值
回退版本
git checkout 0cd0d2e8be90fbf9d6efe6572c23c2d14f32ff0a
成功,控制台打印出了
Release version 5.0.20.RELEASE
2)配置IDEA的gradle
3)导入源码
open->选中目录打开即可
4)开始编译
会发现无法下载一些包。因为repo.spring.io这个站点被限制访问了,所以我们需要配镜像地址
5)配置镜像地址
找到build.gradle这个文件
全文搜索repositories有2处地方,一块是配插件地址的,改成阿里镜像地址
repositories {
gradlePluginPortal()
// maven { url "https://repo.spring.io/plugins-release" }
maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
maven { url "https://maven.aliyun.com/repository/spring-plugin" }
}
还有一块是普通仓库地址,也改成阿里镜像地址
repositories {
mavenCentral()
// maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://maven.aliyun.com/repository/central" }
maven { url "https://maven.aliyun.com/repository/public" }
maven { url 'https://maven.aliyun.com/repository/spring' }
}
注意:4、5步骤不能颠倒,因为有一些包只有先从repo.spring.io下载的才能正常使用。
6)再次编译
过程中会出错
错误1
No such property: values for class: org.gradle.api.internal.tasks.DefaultTaskDependency
直接点open file,把其中一行注释掉
错误2
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
Caused by: java.lang.AbstractMethodError: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar.registerProjectComponents(Lcom/intellij/mock/MockProject;Lorg/jetbrains/kotlin/config/CompilerConfiguration;)V
插件版本太旧去升级下kotlin插件
错误3
单元测试(后面会写),再报错
Error:(20, 50) java: 程序包org.springframework.objenesis.instantiator不存在
cmd进入spring-core所在目录,手动安装jar包
gradle objenesisRepackJar
gradle cglibRepackJar
参考:https://blog.csdn.net/u010999809/article/details/90444714
编写测试模块
新建module->gradle,后面我截个图,贴下代码,具体不写了
package com.cracker.bean;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Person {
private String lastName;
private Integer age;
private String gender;
public Person() {
}
public Person(String lastName, Integer age, String gender) {
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person01" class="com.cracker.bean.Person">
<property name="lastName" value="张三"></property>
<property name="age" value="18"></property>
<property name="gender" value="男"></property>
</bean>
</beans>
package com.cracker.test;
import java.util.List;
import java.util.Map;
import com.cracker.bean.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IOCTest {
@Test
public void test() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
System.out.println("容器启动完成....");
Person bean = (Person) ioc.getBean("person01");
System.out.println(bean);
}
}
结束
然后就可愉快学习spring源码了