快速搭建Spring源码阅读环境(idea)

快速搭建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
运行成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值