spring加载流程之refresh()
转载自
https://blog.csdn.net/yu_kang/article/details/88076796
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. /** * 准备刷新: * 设置启动时间、是否激活标识位 * 初始化属性源(property source)配置 */ prepareRefresh(); // Tell the subclass to refresh the internal bean factory. /** * 获取一个BeanFactory * 拿到DefaultListableBeanFactory,供后面方法调用 */ ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. /** * 对beanFactory进行相关的参数设置,包括classLoader,expressionResolver,注入一些spirng 内部的bean,aware接口等 */ prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. /** * 这个方法在当前版本没有实现 * 可能在spring后面的版本会去扩展 */ postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. /** * 在上下文中调用注册为bean的工厂处理器 * * 添加BeanPostProcessor * 如果发现loadTimeWeaver的Bean */ invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. /** * 注册BeanPostProcessor * 自定义以及spring内部的 */ registerBeanPostProcessors(beanFactory); // Initialize message source for this context. /** * 国际化支持,不关心 */ initMessageSource(); // Initialize event multicaster for this context. /** * 初始化事件监听多路广播器 */ initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. /** * 这个方法在当前版本没有实现 * 可能在spring后面的版本会去扩展 */ onRefresh(); // Check for listener beans and register them. /** * 注册监听器 */ registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. /** * 实例化所有bean */ 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(); } } } }