zoukankan      html  css  js  c++  java
  • spring源码第二章_容器的基本实现

    一、先用一个简单的获取bean实例的例子来了解

      1、类结构如下: 

      

      2、MyTestBean.java为bean对象,实体类,代码如下:

    MyTestBean

      3、BeanFactoryTest代码如下:

    package org.springframework.liulei;
    
    import java.io.IOException;
    import junit.framework.TestCase;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    
    
    /**
     * 
     * @author Liulei
     */
    public class BeanFactoryTest extends TestCase{
    
        @SuppressWarnings("deprecation")
        public void testSimpleLoad() throws IOException{
            System.out.println(BeanFactoryTest.class.getResource("beanFactoryTest.xml"));
            BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml",getClass()));
            MyTestBean bean = (MyTestBean) bf.getBean("myTestBean");
            System.out.println(bean.getTestStr());
        }
        
    }
    BeanFactoryTest

      此处以此类为主要阐述对象:

        BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml",getClass()));

        通过XmlBeanFactory初始化的时序为:

          1、new ClassPathResource:拿到resource资源,此处路径需谨慎,"/*.xml"则是指classpath路径下的文件,"*.xml"是指当前类同项目包下的xml

          2、new XmlBeanFactory:通过XmlBeanDefinitionReader的loadBeanDefinitions(resource)方法返回bean定义的数目

    Wait for the flying eagles Believe that I will succee and just stack to it_Mr.Liu
  • 相关阅读:
    PHP的来龙去脉
    最近动态
    motorola usb driver下载
    C#泛型学习笔记
    Visual Studio 2008 SP1 (Beta)
    【Linux操作系统分析】计算机是怎样工作的
    细嚼慢咽C++primer(1)——指针,void*型指针,const限定符(2)
    初识Hadoop(1)
    2013.05.14_C++primer笔记
    初始Hadoop(3)
  • 原文地址:https://www.cnblogs.com/liu-eagles/p/8099392.html
Copyright © 2011-2022 走看看