intellij idea 搭建spring源码环境

intellij idea 搭建spring源码环境

我原本自己安装了gradle 5.X版本的,后面导入idea出错了。spring源码此时好像是只支持4.x版本的。所以后面我就用idea的gradle了,没用自己本地的。

idea从git克隆源码

先到github上的spring源码上复制https地址

https://github.com/spring-projects/spring-framework.git

在idea下用git拉取分支,如下图所示

在这里插入图片描述

填入git的url,和本地目录位置,等待下载完成。下载完成后有个确认请求,点击yes,选择使用gradle导入

在这里插入图片描述

到这里的时候我发现我的idea里面只有maven,所以需要去设置里面安装gradle插件

在这里插入图片描述

工程属性配置

在这里插入图片描述

导入完成后发现spring-core会报错,缺失了cglib和objenesis的jar包。

打开右边侧边栏gradle,找到spring-core下 Tesks中下的other

在这里插入图片描述

分别点击图中这两个命令

在这里插入图片描述
这里还有个问题,就是AspectJ模块会报错,这个比较麻烦,而且暂时用不到,所以就先不管它,或者将它删除。

创建测试项目

在根目录上右键新建一个module,选择一个模板,这里我用的是图中这个

在这里插入图片描述

然后填groupId和artifactId

接着选择项目所在目录,这里要注意我们测试项目所在的目录和其他的spring-*项目是平级的

我创建后的项目结构是这样的

在这里插入图片描述

几个文件的内容如下

MyTestBean

package org.springframework;

public class MyTestBean {
	private String testStr = "testStr";

	public String getTestStr() {
		return testStr;
	}

	public void setTestStr(String testStr) {
		this.testStr = testStr;
	}
	
}

beanFactoryTest.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="myTestBean" class="org.springframework.MyTestBean"></bean>
</beans>

AppTest

package org.springframework;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;


public class AppTest 
{

    @Test
    public void shouldAnswerWithTrue()
    {
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
		MyTestBean bean = (MyTestBean) beanFactory.getBean("myTestBean");
		assertEquals("testStr",bean.getTestStr());
    }
}

这是一个最简单的bean加载,要运行成功还要有必须的依赖才行

添加依赖

file->project structure->modules->自己的module

在这里插入图片描述

其中大部分都是spring源码中的项目,只有commons-logging是我自己本地加的,你也可以用maven自己下载这个jar包

然后跑我们的test,绿色正确即可。debug也可以去查看源码

发布了29 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43094917/article/details/88092283