Spring容器刷新

org.springframework.context.support.AbstractApplicationContext.refresh()

synchronized (this.startupShutdownMonitor) {
...
1. 容器刷新前的准备 prepareRefresh

2. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()
  创建DefaultListableBeanFactory,若已有beanFacotry则先摧毁再创建;解析bean定义并放入beanFactory;

3. prepareBeanFactory(beanFactory)
  设置beanFactory,加入一些BeanPostprocessor和其他组件对象。

4. postProcessBeanFactory(beanFactory)
  可以添加一些BeanFactoryPostProcessor等

5. invokeBeanFactoryPostProcessors(beanFactory)
  调用 BeanFactoryPostProcessor 
BeanFactoryPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory)
BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(BeanDefinitionRegistry)

6. registerBeanPostProcessors(beanFactory)
  注册 BeanPostProcessor
  BeanPostProcessor#postProcessBeforeInitialization(Object bean, String beanName)
  BeanPostProcessor#postProcessAfterInitialization(Object bean, String beanName)
  InstantiationAwareBeanPostProcessor...

*****************************not important***************************************
7. initMessageSource()
   国际化,registerSingleton(DelegatingMessageSource)
8. initApplicationEventMulticaster()
   事件广播器,registerSingleton(SimpleApplicationEventMulticaster)
9. onRefresh()
10. registerListeners()
    注册事件监听器 
*****************************not important***************************************

11. finishBeanFactoryInitialization(beanFactory)
    初始化singleton beans
12. finishRefresh()
    清除resourceCache,registerSingleton(DefaultLifecycleProcessor),
    广播容器刷新事件(ContextRefreshedEvent)
...
}

一、prepareRefresh(容器刷新前的准备 )

//容器刷新前,设置一些值
    this.startupDate = System.currentTimeMillis();
    this.closed.set(false);
    this.active.set(true);
    
// Initialize any placeholder property sources in the context environment
     initPropertySources();

//获取StandardEnvironment环境,执行一些校验
    getEnvironment().validateRequiredProperties();
//Allow for the collection of early ApplicationEvents,to be published once the multicaster is available...
    this.earlyApplicationEvents = new LinkedHashSet<>();

二、获取BeanFactory

使用xml配置的较老的容器,在这一步会加载beanDefinition。

三、prepareBeanFactory(放入一些组件到BeanFactory)

四、postProcessBeanFactory

模板方法,子类可以做一些拓展实现。

五、invokeBeanFactoryPostProcessors

初始化 BeanFactoryPostProcessor 并调用其方法。

//BeanFactoryPostProcessor
void postProcessBeanFactory(ConfigurableListableBeanFactory)
//BeanDefinitionRegistryPostProcessor
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry)

重要的处理类
org.springframework.context.annotation.ConfigurationClassPostProcessor 负责加载BeanDefinition。

六、registerBeanPostProcessors

初始化 BeanPostProcessor 并添加到BeanFactory中。

//BeanPostProcessor
Object postProcessBeforeInitialization(Object bean, String beanName)
Object postProcessAfterInitialization(Object bean, String beanName)
PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
//InstantiationAwareBeanPostProcessor
Object postProcessBeforeInstantiation(Class beanClass, String beanName)
boolean postProcessAfterInstantiation(Object bean, String beanName)
//MergedBeanDefinitionPostProcessor
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName)

重要的处理类
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor

七、initMessageSource

注入单实例 org.springframework.context.support.DelegatingMessageSource 实现国际化功能。

八、initApplicationEventMulticaster

注入单实例 org.springframework.context.event.SimpleApplicationEventMulticaster.SimpleApplicationEventMulticaster(BeanFactory) 实现事件传播功能

九、onRefresh

模板方法,子类可以做一些拓展实现。

十、registerListeners

注册监听器,

十一、finishBeanFactoryInitialization⭐

实例化所有的 non-lazy-init的BeanDefinition,容器刷新中最核心的方法。
详细内容

十二、finishRefresh

清空一些缓存资源、执行一些LifecycleProcessor、发布一些事件等

你可能感兴趣的:(Spring容器刷新)