ApplicationRunner使用

news/2024/10/21 3:47:59/

本文来说下CommandLineRunner和ApplicationRunner的使用

文章目录

  • ApplicationRunner
    • 使用示例
    • 程序结果
  • CommandLineRunner
    • 使用示例
    • 程序结果
  • ApplicationListener
    • 触发时机
    • 使用实例
    • 程序结果
    • 注意问题


ApplicationRunner

使用起来很简单,只需要实现CommandLineRunner或者ApplicationRunner接口,重写run方法就行。在springboot完全初始化完毕后,会执行CommandLineRunner和ApplicationRunner,两者唯一的区别是参数不同,但是不会影响,都可以获取到执行参数


使用示例

代码实例

package com.wideth.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;/**** 在springboot完全初始化完毕后,* 会执行CommandLineRunner和ApplicationRunner,* 两者唯一的区别是参数不同,但是不会影响,都可以获取到执行参数。*/
@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args){log.info("====>>> MyApplicationRunner.run()正在执行=========");}
}

程序结果

程序结果

在这里插入图片描述


CommandLineRunner

使用示例

代码实例

package com.wideth.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;/**** 在springboot完全初始化完毕后,* 会执行CommandLineRunner和ApplicationRunner,* 两者唯一的区别是参数不同,但是不会影响,都可以获取到执行参数。*/
@Slf4j
@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args){log.info("====>>> MyCommandLineRunner.run()正在执行=========");}
}

程序结果

程序结果

在这里插入图片描述


ApplicationListener

通过事件监听我们也可以实现springboot启动执行方法。实现ApplicationListener,重写onApplicationEvent方法,便可在所有的bean加载完毕后执行。


触发时机

在IOC的容器的启动过程,当所有的bean都已经处理完成之后,spring ioc容器会有一个发布ContextRefreshedEvent事件的动作。


使用实例

使用实例

package com.wideth.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;/**** 在IOC的容器的启动过程,* 当所有的bean都已经处理完成之后,* spring ioc容器会有一个发布* ContextRefreshedEvent事件的动作。*/
@Slf4j
@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {log.info("====>>> MyApplicationListener.onApplicationEvent()正在执行=========");}
}

程序结果

程序结果

在这里插入图片描述


注意问题

系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)

这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理

  //root application context 没有parentif (event.getApplicationContext().getParent() == null) { //逻辑代码}

http://www.ppmy.cn/news/90012.html

相关文章

第三十四回:AlertDialog Widget

文章目录 概念介绍使用方法示例代码 我们在上一章回中介绍了Dismissible Widget相关的内容,本章回中将介绍 AlertDialog Widget.闲话休提&#xff0c;让我们一起Talk Flutter吧。 概念介绍 我们介绍的AlertDialog是指程序中弹出的确认窗口&#xff0c;其实我们在上一章回中删…

网易易盾流量多发反外挂落地实践

背景及目的 环境迁移 反外挂服务建德机房线上迁移&#xff0c;采取的方案是&#xff1a;建德机房独立部署一套完整的集群&#xff0c;统一经由完整的性能测试、故障演练、功能回归后&#xff0c;通过线上分阶段切换流量的方式来切服务。QA 需要比对验证 2 个不同环境下核心接口…

多目标遗传算法NSGA-II改进策略

上篇写道了关于多目标遗传算法NSGA-II原理详解及算法代码实现&#xff0c;本文将继续在这篇文章的基础上更深一步的向前探索&#xff0c;探索方向为&#xff1a; 基于NSGA-II算法的固有缺点&#xff0c;着重对其算法提出改进策略&#xff0c;并予以代码实现。同样&#xff0c;本…

1112 Stucked Keyboard (PAT甲级)

写完后看柳婼的解法&#xff0c;其实可以直接数重复了几次&#xff0c;我这个写法有点复杂化了。 原代码&#xff1a; #include <iostream> #include <string> #include <set>int main(){int k, j;bool flag;std::set<char> st, printed;std::string…

codeforce第874轮(div3)

地址&#xff1a;codeforce第84轮&#xff08;div3&#xff09; A&#xff1a; 题目的意思是&#xff1a;给定我们一堆包含两个字符的字符串&#xff0c;字符串a和字符串b只要a的后一个字母和b的前一个字母相等即可链接&#xff0c;现在给出最后链接好的字符串&#xff0c;问…

〖Web全栈开发⑤〗— CSS基础

〖Web全栈开发⑤〗— CSS基础 (一)CSS基础1.1CSS介绍1.2CSS样式1.3CSS 格式 &#xff08;二&#xff09;CSS 选择器2.1标签选择器2.2类选择器2.3层级选择器2.4id选择器2.5组选择器2.6伪类选择器2.7通配符选择器 &#xff08;三&#xff09;样式表引入3.1外部样式表3.2内部样式表…

二总线-MBus讲解

二总线的叫法演变是从多线到总线再到二总线这么一个过程&#xff0c;尤其在楼宇的消防领域&#xff0c;报警的设备总线基本已经是二总线了&#xff0c;其特点就是电源与通信一起传输&#xff0c;本质上是一个电力载波的思路。那么现在的powerbus二总线又是一个极端&#xff0c;…

知识变现海哥:如何把自己的想法变现?

高手都懂得&#xff0c;简单三招&#xff0c;卖掉自己的想法。 把自己的思维装入别人的大脑&#xff0c;把别人的钱装进自己的口袋。 招一&#xff1a;把已知的事情和知识&#xff0c;变成未知的。 网络自媒体大行其道&#xff0c;你经常会看到听到一些新名词&#xff0c;仔细…