Spring的ApplicationContext实例化源码解析

本文深入解析了Spring的ApplicationContext如何通过ClassPathXmlApplicationContext实例化,并详细介绍了在bean工厂创建、BeanPostProcessor注册及执行过程,包括postProcessBeforeInitialization和postProcessAfterInitialization的顺序。内容涵盖AbstractApplicationContext的refreshBeanFactory、getBeanFactory、postProcessBeanFactory方法,以及BeanFactoryPostProcessor和BeanPostProcessor的作用。
摘要由CSDN通过智能技术生成

Spring的ApplicationContext来获得Bean的操作,通过ApplicationContext context = new ClassPathXmlApplicationContext("spring/test-bean.xml");这个代码进行了调试,于是把整个流程做了整理

在看下面的流程前可以看看下面的类图:


整个流程中,Spring留了很多可以覆盖的方法和需要实现的接口,可以方便我们在整个流程中的适当地方个性化自己的功能。

1.AbstractApplicationContext.java中的protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException

/**
* Subclasses must implement this method to perform the actual configuration load.
* The method is invoked by {@link #refresh()} before any other initialization work.
* <p>A subclass will either create a new bean factory and hold a reference to it,
* or return a single BeanFactory instance that it holds. In the latter case, it will
* usually throw an IllegalStateException if refreshing the context more than once.
* @throws BeansException if initialization of the bean factory failed
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/

       继承AbstractApplicationContext,实现这个方法,在这个方法中构造出自己特有的BeanFactory,可以参照AbstractRefreshableApplicationContext.java对于这个的实现 

       refreshBeanFactory会调用在AbstractRefreshableApplicationContext.java中的loadBeanDefinitions方法,这个方法是抽象的,可以交给具体的实现,比如支持从XML文件获取Bean的配置信息,也可以实现这个方法,在这个方法中从数据库,从远程服务上获取Bean

2.AbstractApplicationContext.java中的public abstract Configurabl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值