Spring Boot 事件/監(jiān)聽機(jī)制

Spring 事件/監(jiān)聽

ApplicationEvent: 應(yīng)用事件
ApplicationListener: 應(yīng)用監(jiān)聽器

Spring Boot 事件/監(jiān)聽

  • ApplicationEnvironmentPreparedEvent
  • ApplicationPreparedEvent
  • ApplicationStartingEvent
  • ApplicationReadyEvent
  • ApplicationFailedEvent

ConfigFileApplicationListener

管理配置文件,例如:

  • application.properties
  • application.yml
  • application-{profile}.yml

Spring 上下文如何控制啟動(dòng)順序

  • 實(shí)現(xiàn)Ordered 接口 以及類標(biāo)識(shí)注解@Order
    1.默認(rèn)最大優(yōu)先級(jí)+10,在Spring里面,越小約優(yōu)先。

Spring Cloud 事件/監(jiān)聽

  • BootstrapApplicationListener
    1.負(fù)責(zé)加載bootstrap.properties或bootstrap.yml
    2.負(fù)責(zé)初始化Bootstrap跟節(jié)點(diǎn): ApplicationContext ID = "bootstrap"
# 跟節(jié)點(diǎn)默認(rèn)叫bootstrap ,可以通過配置修改
ConfigurableApplicationContext contex = builder.run();

1.SpringApplication refresh 調(diào)用裝配Bean
2.SpringBoot通過AbstractApplicationContext 的refresh注入Beans

AbstractApplicationContext

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

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                /** 初始化消息源 **/
                initMessageSource();

                /** 初始化事件監(jiān)聽 **/
                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();
            }
        }
    }
...
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容