public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
@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);
方法进入后首先调用初始化环境中的BeanDefinitionRegistryPostProcessor(非beanDefinitionMap中的)类型的后置处理器的postProcessBeanDefinitionRegistry方法;
然后再根据权限大小执行beanDefinitionMap中实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor类型的后置处理器的postProcessBeanDefinitionRegistry方法(这一步会执行ConfigurationClassPostProcessor的方法去扫描环境中的符合条件的组件注册到beanDefinitionMap里,先执行默认扫描然后执行ImportBeanDefinitionRegistrar的扫描,也就是说用户自定义的拓展点除ImportBeanDefinitionRegistrar之外的都是在此步骤之后被调用);
在此之后的环境中的beanDefinitionMap里已经有了被ConfigurationClassPostProcessor扫描到的beanDefinition
然后再根据权限大小执行beanDefinitionMap中实现Ordered接口的BeanDefinitionRegistryPostProcessor类型的后置处理器的postProcessBeanDefinitionRegistry方法;
然后再根据被扫面的先后顺序执行beanDefinitionMap中没有实现PriorityOrdered以及Ordered接口的BeanDefinitionRegistryPostProcessor类型的后置处理器的postProcessBeanDefinitionRegistry方法;
然后根据实现PriorityOrdered接口、实现Ordered接口、没有实现PriorityOrdered和Ordered接口的对象按优先级或者扫描先后顺序执行beanDefinitionMap中BeanDefinitionRegistryPostProcessor类型的后置处理器的postProcessBeanFactory方法;
然后执行初始化环境中(非beanDefinitionMap中)的BeanFactoryPostProcessor的postProcessBeanFactory方法;
然后再根据权限大小执行实现PriorityOrdered接口的BeanFactoryPostProcessor类型的后置处理器的postProcessBeanFactory方法;
然后再根据权限大小执行实现Ordered接口的BeanFactoryPostProcessor类型的后置处理器的postProcessBeanFactory方法;
然后再根据被扫面的先后顺序执行没有实现PriorityOrdered以及Ordered接口的BeanFactoryPostProcessor类型的后置处理器的postProcessBeanFactory方法;
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
注册一个和消息相关的bean
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
此步骤会扫描环境中加了@Configuration注解的类,并根据此类做一些配置(会创建此类里面使用@Bean注解的方法bean)
// Initialize other special beans in specific context subclasses.
onRefresh();
注册监听器bean
// Check for listener beans and register them.
registerListeners();
此步骤会根据环境中的beanDefinition注册@Component、@Controller、@Service等bean,不包括前面步骤中已经创建的bean
// 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();
}
}
}
}