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文件

  

[html]  copy
 <?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文件

[java]  copy
  1. package bean;  
  2.   
  3. public class SpringTestBean {  
  4.   
  5.     private String testStr = "testStr";  
  6.   
  7.     public String getTestStr() {  
  8.         return testStr;  
  9.     }  
  10.   
  11.     public void setTestStr(String testStr) {  
  12.         this.testStr = testStr;  
  13.     }  
  14.       
  15. }  

       3、新建BeanFactoryTest.java文件

[java]  copy
  1. package bean;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.support.DefaultListableBeanFactory;  
  5. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;  
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  7. import org.springframework.core.io.ClassPathResource;  
  8.   
  9. public class BeanFactoryTest {  
  10.   
  11. public static void main(String[] args) {  
  12.     /* 
  13.     ClassPathResource classPathResource = new ClassPathResource("spring-beans.xml"); 
  14.     DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); 
  15.     XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); 
  16.     reader.loadBeanDefinitions(classPathResource); 
  17.     SpringTestBean springTestBean = (SpringTestBean)factory.getBean("springTestBean"); 
  18.     System.out.println(springTestBean.getTestStr()); 
  19.     */  
  20.       
  21.     BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));  
  22.     SpringTestBean bean = (SpringTestBean)beanFactory.getBean("springTestBean");  
  23.     System.out.println(bean.getTestStr());  
  24.       
  25. }  
  26. }  

        到现在为止,框架已经搭建好了。要是没有问题的话,运行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。
 

       2、 新建SpringTestBean.java文件

[java] view plain copy
  1. package bean;  
  2.   
  3. public class SpringTestBean {  
  4.   
  5.     private String testStr = "testStr";  
  6.   
  7.     public String getTestStr() {  
  8.         return testStr;  
  9.     }  
  10.   
  11.     public void setTestStr(String testStr) {  
  12.         this.testStr = testStr;  
  13.     }  
  14.       
  15. }  

       3、新建BeanFactoryTest.java文件

[java] view plain copy
  1. package bean;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.support.DefaultListableBeanFactory;  
  5. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;  
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  7. import org.springframework.core.io.ClassPathResource;  
  8.   
  9. public class BeanFactoryTest {  
  10.   
  11. public static void main(String[] args) {  
  12.     /* 
  13.     ClassPathResource classPathResource = new ClassPathResource("spring-beans.xml"); 
  14.     DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); 
  15.     XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); 
  16.     reader.loadBeanDefinitions(classPathResource); 
  17.     SpringTestBean springTestBean = (SpringTestBean)factory.getBean("springTestBean"); 
  18.     System.out.println(springTestBean.getTestStr()); 
  19.     */  
  20.       
  21.     BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));  
  22.     SpringTestBean bean = (SpringTestBean)beanFactory.getBean("springTestBean");  
  23.     System.out.println(bean.getTestStr());  
  24.       
  25. }  
  26. }  

        到现在为止,框架已经搭建好了。要是没有问题的话,运行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。

原文地址:https://www.cnblogs.com/ljsy-yjx/p/7458812.html