美文网首页
spring的refresh()

spring的refresh()

作者: huapro | 来源:发表于2019-05-16 16:58 被阅读0次

    refresh()方法是spring的核心,在其中完成了容器的初始化

    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();
                }
            }
        }
    

    1、prepareRefresh();
    容器刷新前的准备,设置上下文状态,获取属性,验证必要的属性等

    2、ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    获取新的beanFactory,销毁原有beanFactory、为每个bean生成BeanDefinition等

    3、prepareBeanFactory(beanFactory);
    配置标准的beanFactory,设置ClassLoader,设置SpEL表达式解析器等

    4、postProcessBeanFactory(beanFactory);
    模板方法,允许在子类中对beanFactory进行后置处理。

    5、invokeBeanFactoryPostProcessors(beanFactory);
    实例化并调用所有注册的beanFactory后置处理器(实现接口BeanFactoryPostProcessor的bean,在beanFactory标准初始化之后执行)。
    例如:
    PropertyPlaceholderConfigurer(处理占位符)

    6、registerBeanPostProcessors(beanFactory);
    实例化和注册beanFactory中扩展了BeanPostProcessor的bean。
    例如:
    AutowiredAnnotationBeanPostProcessor(处理被@Autowired注解修饰的bean并注入)
    RequiredAnnotationBeanPostProcessor(处理被@Required注解修饰的方法)
    CommonAnnotationBeanPostProcessor(处理@PreDestroy、@PostConstruct、@Resource等多个注解的作用)等。

    7、initMessageSource();
    初始化国际化工具类MessageSource

    8、initApplicationEventMulticaster();
    初始化时间广播器

    9、onRefresh();
    模板方法,在容器刷新的时候可以自定义逻辑,不同的Spring容器做不同的事情。

    10、registerListeners();
    注册监听器,广播early application events

    11、finishBeanFactoryInitialization(beanFactory);
    实例化所有剩余的(非懒加载)单例
    比如invokeBeanFactoryPostProcessors方法中根据各种注解解析出来的类,在这个时候都会被初始化。
    实例化的过程各种BeanPostProcessor开始起作用。

    12、finishRefresh();
    refresh做完之后需要做的其他事情。
    清除上下文资源缓存(如扫描中的ASM元数据)
    初始化上下文的生命周期处理器,并刷新(找出Spring容器中实现了Lifecycle接口的bean并执行start()方法)。
    发布ContextRefreshedEvent事件告知对应的ApplicationListener进行响应的操作

    相关文章

      网友评论

          本文标题:spring的refresh()

          本文链接:https://www.haomeiwen.com/subject/thziaqtx.html