栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot事件监听机制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot事件监听机制

监听器模式4要素
  1. 事件
  2. 监听器
  3. 广播器
  4. 触发机制
SpingBoot 实现

系统监听器
org.springframework.context.ApplicationListener

@FunctionalInterface  //只能有一个方法
public interface ApplicationListener extends EventListener {
    void onApplicationEvent(E var1);
}

通过实现这个接口去监听事件,遵循 java.util.EventListener 标准
Spring 事件,触发感兴趣的监听器。当监听到事件发生的时候,去执行具体的事情

系统广播器 管理监听器(添加/删除),广播事件

public interface ApplicationEventMulticaster {
    void addApplicationListener(ApplicationListener var1);

    void addApplicationListenerBean(String var1);

    void removeApplicationListener(ApplicationListener var1);

    void removeApplicationListenerBean(String var1);

    void removeAllListeners();

    void multicastEvent(ApplicationEvent var1);

    void multicastEvent(ApplicationEvent var1, @Nullable ResolvableType var2);
}

Spring系统事件
org.springframework.boot.context.event.ApplicationPreparedEvent

public class ApplicationPreparedEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;

    public ApplicationPreparedEvent(SpringApplication application, String[] args, ConfigurableApplicationContext context) {
        super(application, args);
        this.context = context;
    }

    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
}



事件发送顺序

Starting ApplicationStartingEvent 框架一启动就发出
EnvironmentPrepared ApplicationEnvironmentPreparedEvent 环境准备好,spring容器的系统属性和指定的属性已加载到容器内
ContextInitialized ApplicationContextInitializedEvent 启动并且准备好了应用上下文 加载Bean 定义之前发布的
Prepared ApplicationPreparedEvent 应用上下文已经创建完毕,但Bean 还没完全加载完成
Started ApplicationStartedEvent 把Bean 实例化完成,但还没有调用 ApplicationRunner 和 CommandLineRunner 扩展接口
Ready ApplicationReadyEvent 调用完 ApplicationRunner 和 CommandLineRunner 扩展接口之后
Faild ApplicationFailedEvent 过程中出现失败

SpringBoot 启动流程

    public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

setListeners方法,就是将所有配置在spring.factories文件中,key值为 org.springframework.context.ApplicationListener的类实例化后,设置到SpringApplication对象的listeners属性当中。
没有任何添加的情况下,分别从不同的spring依赖中获取

spring.factories

Application Listeners
org.springframework.context.ApplicationListener=
org.springframework.boot.ClearCachesApplicationListener,
org.springframework.boot.builder.ParentContextCloserApplicationListener,
org.springframework.boot.context.FileEncodingApplicationListener,
org.springframework.boot.context.config.AnsiOutputApplicationListener,
org.springframework.boot.context.config.ConfigFileApplicationListener,
org.springframework.boot.context.config.DelegatingApplicationListener,
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,
org.springframework.boot.context.logging.LoggingApplicationListener,
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
  private  Collection getSpringFactoriesInstances(Class type, Class[] parameterTypes, Object... args) {
        ClassLoader classLoader = this.getClassLoader();
        Set names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
        List instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
        AnnotationAwareOrderComparator.sort(instances);
        return instances;
    }
监听器事件触发机制

以分析 Starting 为例
待完成
4-4 恍然大悟:监听事件触发机制解析上 2020-1-11 18-45-13

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号