方法內(nèi)容:
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
實現(xiàn)一:org.springframework.context.support.AbstractRefreshableApplicationContext.class
refreshBeanFactor()代碼
@Override
protected final void refreshBeanFactory() throws BeansException {
//如果有beanFactory先銷毀
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();//創(chuàng)建新工廠
beanFactory.setSerializationId(getId());//設置serializationId
customizeBeanFactory(beanFactory);//設置beanFactory
loadBeanDefinitions(beanFactory);//加載BeanDefinitions
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
1、先銷毀原有beanFactory;
2命咐、創(chuàng)建并初始化BeanFactory;
3、加載BeanDefinitions至朗。
實現(xiàn)二:org.springframework.context.support.GenericApplicationContext.class
refreshBeanFactor()代碼
@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());
}
僅設置beanFactory的serializationId;
那么,這個beanFactory是在什么時候?qū)嵗拇煞縝eanDefinitions是在什么地方加載的仰税?
beanFactory實例化:
beanFactory是在GenericApplicationContext
的默認構造函數(shù)里實例化的(DefaultListableBeanFactory
)
加載beanDefinitions:
GenericApplicationContext
的子類在構造函數(shù)里加載beanDefinitions