获取源码地址
进入GitHub,https://github.com/spring-projects/spring-framework,获取git地址。
下载源码
1、IDEA 下 Spring Git 拉取分支,选择Git
2、复制git地址,选择安装目录,点击Clone
3、等待拉取代码
4、导入IDEA,选择Gradle,点击下一步,Gradle home选择安装Gradle的目录,点击finish
5、导入成功
创建测试模块
1、右键新建一个module
2、选择Gradle,点击Next
3、输入模块名称,点击Next
开始测试
1、创建测试类时,有些包会不存在,所以先在Gradle中添加依赖。选择my-test模块的gradle文件,添加依赖
compile project(":spring-beans")
compile project(":spring-core")
1、创建一个bean
// An highlighted block
package com.guiji.bean;
public class MyTestBean {
private String testStr = "javaGuiji";
public String getTestStr () {
return testStr;
}
public void setTestStr(String testStr){
this.testStr = testStr;
}
}
2、创建xml文件
// An highlighted block
<?xml version="1.0" encoding="ISO-8859-1"?>
<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-3.0.xsd">
<bean id="myTestBean" class="com.guiji.bean.MyTestBean">
</bean>
</beans>
4、创建测试类
// An highlighted block
package com.guiji.test;
import com.guiji.bean.MyTestBean;
import org.junit.Assert;
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 BeanFactoryTest {
@Test
public void testSimpleLoad(){
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("BeanFactoryTest.xml"));
MyTestBean myTestBean = (MyTestBean) beanFactory.getBean("myTestBean");
String testStr = myTestBean.getTestStr();
System.out.println(testStr);
Assert.assertEquals("javaGuiji",testStr);
}
}
5、整个模块目录结构
6、运行测试类,出现报错
这是因为缺少cglib和objenesis包导致的,在idea中打开gradle,找到spring-core下的Tasks,在other中点击cglibRepackJar和objenesisRepackJar。
继续运行报如下错
解决办法如下图,设置测试运行器
再次运行报如下错
junit4.11以上版本不在包含hamcrest,引入hamcrest-core-1.3.jar或者降低junit版本,我这边是降低junit版本为4.10
再次运行,终于成功了
到此环境已经搭建好了,下面就可以开始进行源码分析了。