快速搭建Spring源码阅读环境
安装gradle环境
前提有gradle环境
参考安装博客
克隆Spring源码
GitHub的Spring项目
从GitHub上面克隆或许太慢了,可以使用码云进行拉取后,再进行克隆
安装
构建gradle项目
项目目录向下执行gradlew.bat
配置镜像源
找到项目目录的build.gradle
为了可以快速下载相关的包,最好加上国内的镜像源
repositories {
mavenCentral()
maven { url "https://maven.aliyun.com/repository/public" }
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
idea配置
idea配置好gradle工具后,配置导入项目,gradle工具自动下载包
下载完成后,进行build project
完成后,源码下面应该是后build的文件夹
里面是编译好的class文件
测试
搭建好阅读环境后,写一个测试用例
建立一个子模块
配置好需要用到的模块
找到创建好的模块,找到模块目录下的build.gradle
plugins {
id 'java'
}
group 'org.springframework'
version '5.2.15.BUILD-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
运行测试
我创建了这些文件
// Group.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Group implements ApplicationContextAware, BeanFactoryAware {
private String group_name;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
public String getGroup_name() {
return group_name;
}
public void setGroup_name(String group_name) {
this.group_name = group_name;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
}
// User.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class User implements ApplicationContextAware, BeanFactoryAware {
private String name;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
}
// MyTest.java
import com.czy.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User bean = context.getBean(User.class);
System.out.println(bean.getName());
}
}
配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.czy.bean.User">
<property name="name" value="lisi"></property>
</bean>
<bean id="group" class="com.czy.bean.Group">
<property name="group_name" value="abc"></property>
</bean>
</beans>
启动MyTest
运行成功