spring1.0初探之容器初始化过程解析

上一篇我们说到了spring1.0的基本配置使用,特别需要注意的是spring1.0的bean.xml文件是没有名称空间的,只有文档的定义信息。那么本篇,我们来看一下spring1.0版本的容器是如何初始化的,bean是怎么创建出来的,篇幅较长,请大家耐心阅读,如果有什么错误的地方,欢迎大家指正。无论是spring1.0还是spring5.0,其核心思想都离不开容器的初始化,bean的注入,后置处理起的使用等等功能,那么spring后面的版本提供了很多复杂的功能,这对我们阅读源码有一定的影响,而sp
摘要由CSDN通过智能技术生成

上一篇我们说到了spring1.0的基本配置使用,特别需要注意的是spring1.0的bean.xml文件是没有名称空间的,只有文档的定义信息。

那么本篇,我们来看一下spring1.0版本的容器是如何初始化的,bean是怎么创建出来的,篇幅较长,请大家耐心阅读,如果有什么错误的地方,欢迎大家指正。

无论是spring1.0还是spring5.0,其核心思想都离不开容器的初始化,bean的注入,后置处理器的使用等等功能,那么spring后面的版本提供了很多复杂的功能,这对我们阅读源码有一定的影响,而spring1.0所提供的功能比较简单,阅读起来也比较方便。

好了,话不多说,我们先来看看上一篇里面,创建容器的的方法

public class MySpringTest01 {

    private ClassPathXmlApplicationContext applicationContext;

    @Before
    public void getContext(){
        //容器初始化
        applicationContext = new ClassPathXmlApplicationContext("bean01.xml");
    }

    @Test
    public void test1(){
        Person person1 = (Person) applicationContext.getBean("person");
        System.out.println("person1:" + person1);
        Person person2 = (Person) applicationContext.getBean("person");
        System.out.println("person2:" + person2);
        System.out.println(person1 == person2);
    }
}

下面我们来看一下ClassPathXmlApplicationContext这个类里面干了什么事情

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
    private String[] configLocations;
    //这个就是我们上一步创建对象时调用的方法,传入了一个bean.xml地址的参数
    public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
        //将bean.xml文件地址保存在configLocations里面
        this.configLocations = new String[]{configLocation};
        //调用刷新容器方法
        this.refresh();
    }

    public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
        this.configLocations = configLocations;
        this.refresh();
    }

    public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
        super(parent);
        this.configLocations = configLocations;
        this.refresh();
    }

    protected String[] getConfigLocations() {
        return this.configLocations;
    }
}

我们先看下ClassPathXmlApplicationContext这个类的继承关系

可以看到ClassPathXmlApplicationContext是继承自AbstractXmlApplicationContext的,而AbstractXmlApplicationContext又继承自AbstractApplicationContext

我们看下每个父类中的方法

 

 

从图中我们可以看到ClassPathXmlApplicationContext调用的refresh()方法,其实就是调用父类AbstractApplicationContext的refresh()方法,那我们现在主要就是分析这个方法的执行过程

    public void refresh() throws BeansException {
		//获取当前时间
        this.startupTime = System.currentTimeMillis();
		//创建beanFactory容器,同时解析bean.xml文件,并将bean的定义信息保存起来
        this.refreshBeanFactory();
		//获取当前容器
        ConfigurableListableBeanFactory 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tong472687551

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值