Day 17 springboot 监听机制

1 Java 监听机制

springboot监听机制,其实是对java提供的事件监听机制的封装。

Java中的事件监听机制定义了以下几个角色:

  • 事件:Event,继承EventObject类,例如点击,拖动等等。

  • 事件源:Source,任意对象的Object

  • 监听器:Listener,实现EventListener接口对象,用来监听事件源上是否有事件

2 springboot 监听机制

springboot项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口用来处理我们自己的逻辑。常见接口:

  • ApplicationContextlnitializer

  • SpringApplicationRunListener

  • CommandLineRunner

  • ApplicationRunner

CommandLineRunner 和ApplicationRunner
1、不需要额外配置,仅仅实现该接口
2、实现该接口,重写run方法。
3、项目启动后,方法执行。

ApplicationContextlnitializer

作用:项目启动时,准备一些资源。

步骤

  1. 需要创建MATE-INF/spring.factories

  1. 配置org.springframework.context.ApplicationContextInitializer=\com.msf.listener.MyApplicationContextInitializer

结果

SpringApplicationRunListener

作用:spring声明周期相关的监听接口;

步骤:与ApplicationContextlnitializer配置相同

结果

简单的了解springboot的事件监听机制了,我们需要用它们做一些事情。

3 springboot流程分析

3.1 创建SpringApplication对象

在debug形式下启动项目

3.2执行run方法

public ConfigurableApplicationContext run(String... args) {
        long startTime = System.nanoTime();  // 计时
        DefaultBootstrapContext bootstrapContext = createBootstrapContext();
        ConfigurableApplicationContext context = null;
        configureHeadlessProperty();
        // !!!获取所有的listener!
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting(bootstrapContext, this.mainApplicationClass);
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 环境准备
            ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            // 环境信息准备
            configureIgnoreBeanInfo(environment);
            // 打印图标
            Banner printedBanner = printBanner(environment);
            // !!!IOC容器创建!
            context = createApplicationContext();
            context.setApplicationStartup(this.applicationStartup);
            prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            // 刷新 context
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
            }
            listeners.started(context, timeTakenToStartup);
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, listeners);
            throw new IllegalStateException(ex);
        }
        try {
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            listeners.ready(context, timeTakenToReady);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }

重点代码:

  1. 获取所有的listener!

SpringApplicationRunListeners listeners = getRunListeners(args);

b. 环境准备

ConfigurableEnvironment environment = prepareEnvironment

c. 创建IoC容器

context = createApplicationContext();

d. 刷新IoC容器

refreshContext(context);

整个流程如下图所示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜转转m

一毛钱助力博主实现愿望

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值