【spring】源码解析之 AbstractApplicationContext

实例化的工作会在容器启动后过AbstractApplicationContext中reflash方法自动进行

AbstractApplicationContext里的reflash方法是Spring初始IOC容器一个非常重要的方法,不管你是ApplicationContext哪个实现类,最终都会进入这个方法。

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();
		}
	}
}

转载于:https://my.oschina.net/wangjunBlog/blog/1816700

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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()方法,创建或获取新的BeanFactoryBeanFactorySpringBean容器,它负责管理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、付费专栏及课程。

余额充值