New ClassPathXmlApplicationContext经历了哪些事情

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:NonOsgiConsumerSample.xml");


这是一段很简单的ApplicationContext初始化的代码,但是在这段代码的背后,springframework又为我们做了哪些事情,使得我们能够从容器中方便的获取我们想要的bean?

跟踪代码我们发现在创建一个ClassPathXmlApplicationContext的过程中,它主要经历了以下几个步骤


ClassPathXmlApplicationContext(String configLocation) ----> 
ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) ----->
AbstractXmlApplicationContext(ApplicationContext parent) ---->
AbstractRefreshableConfigApplicationContext(ApplicationContext parent)---->
AbstractRefreshableApplicationContext(ApplicationContext parent) ->
AbstractApplicationContext(ApplicationContext parent)
//最终在AbstractApplicationContext.refresh()完成bean初始化
AbstractApplicationContext.refresh()

AbstractXmlApplicationContext实现了loadBeanDefinitions() ,它的子类只需实现getConfigResources()

	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
		// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

		// Configure the bean definition reader with this context's
		// resource loading environment.
		beanDefinitionReader.setResourceLoader(this);
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

		// Allow a subclass to provide custom initialization of the reader,
		// then proceed with actually loading the bean definitions.
		initBeanDefinitionReader(beanDefinitionReader);
		loadBeanDefinitions(beanDefinitionReader);
	}

 AbstractRefreshableApplicationContext支持多重刷新,每次在内部会创建一个新的bean factory实例,子类只需实现loadBeanDefinitions()

//AbstractRefreshableApplicationContext关键代码	
protected final void refreshBeanFactory() throws BeansException {
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			customizeBeanFactory(beanFactory);
			loadBeanDefinitions(beanFactory);
			synchronized (this.beanFactoryMonitor) {
				this.beanFactory = beanFactory;
			}
		}
		catch (IOException ex) {
			throw new ApplicationContextException(
					"I/O error parsing XML document for application context [" + getDisplayName() + "]", ex);
		}
	}

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.调用了refreshBeanFactory()
			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);  //无需关注,在webapplicationcontext可能用到

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);
                                                                //实例化和调用所有实现了BeanFactoryPostProcessor的bean
				// 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) {
				// Destroy already created singletons to avoid dangling resources.
				beanFactory.destroySingletons();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}
		}
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值