spring4.0 源码分析 搭建简单的分析环境(一)

一、软件版本

        spring-framework-4.0.7.RELEASE-dist

        jdk1.7.0.79(本人自己使用的)

        myeclipse9.1(本人自己使用的)

二、搭建测试工程

       打开eclipse,新建java工程 SpringSource,建立目录结构。导入spring的jar包,分别为CORE包和Beans包,当然还有一个Log的依赖包。

               spring-core-4.0.7.RELEASE.jar

               spring-beans-4.0.7.RELEASE.jar

               commons-logging-1.1.3.jar

       编写测试用例

       1、 新建spring-beans.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="springTestBean" class="bean.SpringTestBean"/>
</beans>

       2、 新建SpringTestBean.java文件

package bean;

public class SpringTestBean {

	private String testStr = "testStr";

	public String getTestStr() {
		return testStr;
	}

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

       3、新建BeanFactoryTest.java文件

package bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BeanFactoryTest {

public static void main(String[] args) {
	/*
	ClassPathResource classPathResource = new ClassPathResource("spring-beans.xml");
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.loadBeanDefinitions(classPathResource);
	SpringTestBean springTestBean = (SpringTestBean)factory.getBean("springTestBean");
	System.out.println(springTestBean.getTestStr());
	*/
	
	BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
	SpringTestBean bean = (SpringTestBean)beanFactory.getBean("springTestBean");
	System.out.println(bean.getTestStr());
	
}
}

        到现在为止,框架已经搭建好了。要是没有问题的话,运行BeanFactoryTest.java这个类的main方法就可以得出结果。

       在企业级的应用中一般会用ApplicationContext。在这里使用了BeanFactory这个接口,当然在spring4.0中,官方也不建议只用XmlBeanFactory这个类了。所以在注释中我用了另一个方式取代了XmlBeanFactory这个类,但是为了和spring3.2之前的版本衔接上,所以还是用XmlBeanFactory来说明一下。下面我们来分析下上面的测试程序作了什么工作:

       1、 首先是程序读取xml配置文件spring-beans.xml

       2、 根据spring-beans.xml中的配置找到对应的类的配置(也就是xml文件中配置的bean),并且实例化

       3、 调用第二步被实例化的对象的方法。

       这个貌似很简单的过程,spring作了哪些工作,作为一个风靡java程序界的优秀源码真的就是这么简单吗?

       通过这个简单的测试工程,应该大致了解了beans工程的结构,下一次进入注释掉的代码的了解中,也就是spring的两个核心的类DefaultListableBeanFactory和XmlBeanDefinitionReader。
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值