Spring的ApplicationContext实例化源码解析

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
refresh()方法是ApplicationContext接口中定义的方法,它的实现在AbstractApplicationContext抽象类中。 下面是refresh()方法的源码解析: ``` public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } } ``` refresh()方法主要分为以下几个步骤: 1.调用prepareRefresh()方法,准备刷新应用程序上下文。 2.调用obtainFreshBeanFactory()方法,创建或获取新的BeanFactory。BeanFactory是SpringBean容器,它负责管理Bean的生命周期。 3.调用prepareBeanFactory()方法,对BeanFactory进行一些准备工作,例如设置类加载器、添加属性编辑器等。 4.调用postProcessBeanFactory()方法,允许子类在BeanFactory初始化完成后对BeanFactory进行进一步的处理。 5.调用invokeBeanFactoryPostProcessors()方法,对注册在应用程序上下文中的BeanFactoryPostProcessor进行处理。 6.调用registerBeanPostProcessors()方法,注册BeanPostProcessor,这些BeanPostProcessor用于在Bean实例化之后执行一些操作。 7.调用initMessageSource()方法,初始化消息源,用于国际化。 8.调用initApplicationEventMulticaster()方法,初始化事件广播器,用于事件的发布和监听。 9.调用onRefresh()方法,允许子类在应用程序上下文刷新完成后执行一些特殊的操作。 10.调用registerListeners()方法,注册事件监听器。 11.调用finishBeanFactoryInitialization()方法,实例化所有的singleton bean。 12.调用finishRefresh()方法,完成应用程序上下文的刷新。 13.如果在刷新过程中出现异常,会调用destroyBeans()方法销毁已经创建的singleton Bean,然后抛出异常。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值