spring源码阅读环境搭建
一、我的环境
1、系统版本
MacOS 10.15.2
2、开发版本工具
spring-framework-5.0.x.RELEASE
gradle4.9
jdk1.8.0_121
IntelliJ IDEA 2019
假设以上软件都已安装完成。
二、源码下载
1、地址
https://github.com/spring-projects/spring-framework/tags
2、下载完成直接解压到合适的目录即可
3、导入idea
spring是Gradle编译的,所以选择Gradle项目导入。
三、源码编译
1、点击gradle的Refresh按钮
可能会报一些错,可以自行百度进行解决
2、编译开始(执行图中所示的步骤1和步骤2)
依次编译spring-core,spring-oxm,spring-context,spring-bean即可。
四、编写调试demo1在这里插入图片描述
一路next,直到finish即可。
编写demo开始,添加工程依赖
测试类
@SuppressWarnings("deprecated")
public class BeanFactoryTest {
@Test
public void testLoad(){
//方式1,使用XmlBeanFactory方式进行加载
Resource resource= new ClassPathResource("beanFactoryTest.xml");
BeanFactory bf = new XmlBeanFactory(resource);
MyTestBean myTestBean = (MyTestBean)bf.getBean("myTestBean");
//方式2, 使用ApplicationContext方法进行加载,applicationContext包含了beanFactory中的所有方法,并对功能进行了扩展
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanFactoryTest.xml");
//MyTestBean myTestBeanbak = (MyTestBean)applicationContext.getBean("myTestBean");
System.out.println(myTestBean.getTestStr());
}
}
配置文件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="bean.myFactory.MyTestBean"/>
<bean id="getBeanTest" class="bean.myFactory.user.GetBeanTest">
<lookup-method name="getBean" bean="teacher"></lookup-method>
</bean>
<bean id="teacher" class="bean.myFactory.user.Teacher"></bean>
<bean id="testChangeMethod" class="bean.myFactory.replaced_method.TestChangeMethod">
<replaced-method name="changeMe" replacer="replacer"></replaced-method>
</bean>
<!--测试事件监听-->
<bean id="testListener" class="bean.myFactory.event.TestListener"/>
<bean id="replacer" class="bean.myFactory.replaced_method.TestMethodReplacer"></bean>
</beans>
至此,阅读源码的环境已经完成。