Spring Boot 事件/监听机制

Spring 事件/监听

ApplicationEvent: 应用事件
ApplicationListener: 应用监听器

Spring Boot 事件/监听

  • ApplicationEnvironmentPreparedEvent
  • ApplicationPreparedEvent
  • ApplicationStartingEvent
  • ApplicationReadyEvent
  • ApplicationFailedEvent

ConfigFileApplicationListener

管理配置文件,例如:

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

Spring 上下文如何控制启动顺序

  • 实现Ordered 接口 以及类标识注解@Order
    1.默认最大优先级+10,在Spring里面,越小约优先。

Spring Cloud 事件/监听

  • BootstrapApplicationListener
    1.负责加载bootstrap.properties或bootstrap.yml
    2.负责初始化Bootstrap跟节点: ApplicationContext ID = "bootstrap"
# 跟节点默认叫bootstrap ,可以通过配置修改
ConfigurableApplicationContext contex = builder.run();

1.SpringApplication refresh 调用装配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();

                /** 初始化事件监听 **/
                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();
            }
        }
    }
...
}

你可能感兴趣的:(Spring Boot 事件/监听机制)