Day 17 springboot 监听机制
创始人
2024-05-22 02:34:16
0次

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);// 刷新 contextrefreshContext(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);

整个流程如下图所示

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...