1. 容器的刷新
refresh是容器初始化的核心方法,接下来我们一个个分析其中的方法
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
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);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// 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) {
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();
}
}
}
2. BeanFactory准备(prepareRefresh)
请参考Spring注解--容器创建源码(二):BeanFactory准备工作
3. 注册并执行BeanFactoryPostProcessor
请参考Spring注解--容器创建源码(三): BeanFactoryPostProcessor
4. 注册BeanPostProcessors
请参考Spring注解--AOP原理(三):BeanPostProcessor创建与注册
5. initMessageSource
该方法用来做国际化功能,消息绑定,消息解析的功能
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
可以通过@Autowired的方式取得该组件
5. initApplicationEventMulticaster:初始化事件分发器
初始化与监听器有关的组件。请参考Spring注解--监听事件:ApplicationListener
6. onRefresh
该方法留给子类去自定义容器刷新的方法
7. registerListeners
将容器中的监听器添加到事件分发器中。请参考Spring注解--监听事件:ApplicationListener
8.finishBeanFactoryInitialization(beanFactory)
初始化剩下的所有的单实例bean。请参考请参考Spring注解--AOP原理(三):BeanPostProcessor创建与注册
9. finishRefresh
protected void finishRefresh() {
// Initialize lifecycle processor for this context.
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
- initLifecycleProcessor:初始化和生命周期有关的BeanPostProcessor
- getLifecycleProcessor().onRefresh(); 回调生命周期的函数
- publishEvent 发布事件
网友评论