Spring5.3.10
基于AnnotationConfigApplicationContext
本文是AnnotationConfigApplicationContext容器初始化的一部分是针对AbstractApplicationContext#refresh()中下面这行代码的拆解
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
AnnotationConfigApplicationContext类图
通过类图得发现:AnnotationConfigApplicationContext extends GenericApplicationContext
确认后续的AbstractApplicationContext抽象方法的实现
BeanFactory的创建
//AbstractApplicationContext#refresh()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//AbstractApplicationContext#obtainFreshBeanFactory()
/**
* Tell the subclass to refresh the internal bean factory.
* @return the fresh BeanFactory instance
* @see #refreshBeanFactory()
* @see #getBeanFactory()
*/
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
AbstractApplicationContext类中refreshBeanFactory()与getBeanFactory() 都是抽象方法,需要子类实现
从AnnotationConfigApplicationContext类图知道AnnotationConfigApplicationContext extends GenericApplicationContext,所以这里的两个方法是GenericApplicationContext中的方法
//GenericApplicationContext#refreshBeanFactory,官方注释这个方法Do nothing,可忽略
/**
* Do nothing: We hold a single internal BeanFactory and rely on callers
* to register beans through our public methods (or the BeanFactory's).
* @see #registerBeanDefinition
*/
@Override
protected final void refreshBeanFactory() throws IllegalStateException {
if (!this.refreshed.compareAndSet(false, true)) {
throw new IllegalStateException(
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
}
this.beanFactory.setSerializationId(getId());
}
//GenericApplicationContext#getBeanFactory
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
return this.beanFactory;
}
GenericApplicationContext的构造方法中new DefaultListableBeanFactory()