@Override publicvoidrefresh()throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. //initPropertySources(初始化propertySources)->validateRequiredProperties( ConfigurablePropertyResolver#setRequiredProperties) prepareRefresh();
// Tell the subclass to refresh the internal bean factory. //refreshBeanFactory(主要loadBeanDefinitions 解析xml, 转换成beanDefinition)->getBeanFactory //返回DefaultListableBeanFactory 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. //注册web servlet相关处理类的实例 postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context. //PostProcessorRegistrationDelegate注册后处理代理类(BeanDefinitionRegistryPostProcessor), 主要初始化两个类ConfigurationClassPostProcessor(处理配置自动扫描注解)和PropertyPlaceholderConfigurer(替换点位符)) invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory);
// Initialize message source for this context. //初始化messageSource initMessageSource();
// Initialize event multicaster for this context. //初始化事件广播,如果不包含applicationEventMulticaster bean定义就使用默认的SimpleApplicationEventMulticaster initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses. //初始化主题资源,没有就采用默认 onRefresh();
// Check for listener beans and register them. //用于注册监听到事件广播 ,必须实现org.springframework.context.ApplicationListener接口 registerListeners();
// Instantiate all remaining (non-lazy-init) singletons. //所有定义的单例bean全部初始化 finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event. //初始化生命周期上下文事件 finishRefresh(); }
catch (BeansException 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(); } } }
// Register BeanPostProcessorChecker that logs an info message when // a bean is created during BeanPostProcessor instantiation, i.e. when // a bean is not eligible for getting processed by all BeanPostProcessors. int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>(); List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>(); List<String> orderedPostProcessorNames = new ArrayList<String>(); List<String> nonOrderedPostProcessorNames = new ArrayList<String>(); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); priorityOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } elseif (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } }
// First, register the BeanPostProcessors that implement PriorityOrdered. sortPostProcessors(beanFactory, priorityOrderedPostProcessors); registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered. List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>(); for (String ppName : orderedPostProcessorNames) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); orderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } sortPostProcessors(beanFactory, orderedPostProcessors); registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors. List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>(); for (String ppName : nonOrderedPostProcessorNames) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); nonOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors. sortPostProcessors(beanFactory, internalPostProcessors); registerBeanPostProcessors(beanFactory, internalPostProcessors);