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

整个流程如下图所示

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...