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