细谈Bean的实例化过程
文章目录
我们都知道,在执行以下的一行代码后,我们就可以调用在knight.xml文件中配置的bean了。
那么context的内部究竟长什么样?它又是在哪里,以何种方式实现了依赖注入?
ApplicationContext context = new ClassPathXmlApplicationContext("knight.xml");
以ClassPathXmlApplicationContext为例追溯Bean的实例化过程
声明:源代码为Spring 5.0版本
ClassPathXmlApplicationContext的继承与实现关系
DefaultListableBeanFactory的继承与实现关系
ClassPathXmlApplicationContext的构造函数
首先查看ClassPathXmlApplicationContext的构造函数:
// org.springframework.context.support.ClassPathXmlApplicationContxt
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {
configLocation}, true, null);
}
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
可以看到,在其构造函数中,调用了refresh()方法。该方法定义在ClassPathXmlApplicationContext的父父…父接口AbstractApplicationContext中:
// org.springframework.context.support.AbstractApplicationContext
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory. (1)
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);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// 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. (2)
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();
contextRefresh.end();
}
}
}
我们知道,对Bean的实例化操作是由BeanFactory实现的,ApplicationContext在此之外扩展了一些功能。在refresh()方法中和bean实例化相关的代码主要为(1),(2)两行。
我们还知道,BeanFactory初始化Bean分为两步
第一步是容器的启动阶段:容器加载Configuration MetaData,使用工具类(BeanDefinitionReader)解析元数据,注册BeanDefinition到相应的BeanDefinitionRegistry。
第二步是Bean的实例化阶段:当某个请求调用getBean()方法时,根据BeanDefinition实例化这个Bean。
AbstractContext的refresh方法中这两行代码正好对应了这两个操作:
第一行代码初始化一个BeanFactory,用它来注册所有的BeanDefinition
第二行代码集中调动getBean方法,实例化所有Bean
(这也是为什么BeanFactory延迟初始化,而ApplicationContext在一开始就初始化)
注册BeanDefinition的逻辑所在
我们先看ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
这行:
// org.springframework.context.support.AbstractApplicationContext
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
getBeanFactory()就是返回类中定义的BeanFactory属性,我们主要关注refreshBeanFactory()方法
该方法的实现写在AbstractRefreshableApplicationContext类里:
(搞不清这几个类之间关系的可以拉到前面再看一遍类图)
// org.springframework.context.support.AbstractRefreshableApplicationContext
/**
* This implementation performs an actual refresh of this context's underlying
* bean factory, shutting down the previous bean factory (if any) and
* initializing a fresh bean factory for the next phase of the context's lifecycle.
*/
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
在refreshBeanFactory()方法中,首先判断是否已经初始化了一个BeanFactory,若有,就destroy掉。
先创建了一个DefaultListableBeanFactory对象,然后调用loadBeanDefinitions()方法
我们先看loadBeanDefinitions()方法,该方法的实现和你使用哪种Configuration MetaData有关,我们使用Xml文件加载配置,方法的实现写在AbstractXmlApplicationContext类中:
// org.springframework.context.support.AbstractXmlApplicationContext
/**
* Loads the bean definitions via an XmlBeanDefinitionReader.
*@seeorg.springframework.beans.factory.xml.XmlBeanDefinitionReader
*@see#initBeanDefinitionReader
*@see#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, 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.setEnvironment(this.getEnvironment());
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);
}
/**
* Initialize the bean definition reader used for loading the bean
* definitions of