当前位置: 首页 > news >正文

SpringBoot学习笔记四

SpringBoot学习笔记四-监听机制

  • 1. SpringBoot监听器
    • 1.1 无需配置
      • 1.1.1 CommandLineRunner使用
      • 1.1.2 ApplicationRunner的使用
      • 1.1.3 CommandLineRunner与ApplicationRunner的区别
    • 1.2 需要创建META-INF文件,并在其中创建spring.factories,配置相关的信息
      • 1.2.1 ApplicationContextInitialize
      • 1.2.2 SpringApplicationRunListener

1. SpringBoot监听器

在这里插入图片描述上述的四种监听器按照使用的方式可以分为两种:

1.1 无需配置

  • CommandLineRunner
  • ApplicationRunner:还需要写一个有参的构造函数
@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner...run");System.out.println(Arrays.asList(args));}
}
@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("MyApplicationRunner...run");System.out.println(Arrays.asList(args.getSourceArgs()));}
}

项目启动时,会自动执行上述的内容。

1.1.1 CommandLineRunner使用

日常开发中有可能需要实现项目启动后执行的功能,比如特殊数据处理,权限控制、缓存预热等
按照使用可以分为单个实现类和多个实现类,

  • 单个实现类如上面,无需指定执行顺序;
  • 多个实现类如果需要指定执行顺序,需要使用@Order注解来表明执行顺序
@Component
@Order(3)
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner...run");System.out.println(Arrays.asList(args));}
}

@Component
@Order(2)
public class MyCommandLineRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner1...run1");System.out.println(Arrays.asList(args));}
}
@Component
@Order(1)
public class MyCommandLineRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner2...run2");System.out.println(Arrays.asList(args));}
}

执行结果:
在这里插入图片描述注解@Order的执行级别是按照1最优先执行,后面依次执行。

1.1.2 ApplicationRunner的使用

spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法。

run方法的参数: ApplicationArguments可以获取到当前项目执行的命令参数。(比如把这个项目打成jar执行的时候,带的参数可以通过ApplicationArguments获取到)
若有多个代码段需要执行,可用@Order注解设置执行的顺序。

1.1.3 CommandLineRunner与ApplicationRunner的区别

  • CommandLineRunner的方法参数是原始的参数,未做任何处理;
  • ApplicationRunner的参数为ApplicationArguments对象,是对原始参数的进一步封装。

1.2 需要创建META-INF文件,并在其中创建spring.factories,配置相关的信息

  • ApplicationContextInitialize
  • SpringApplicationRunListener

1.2.1 ApplicationContextInitialize

目前不去细研究,有兴趣可以看这篇博客:Springboot扩展点之ApplicationContextInitializer

1.2.2 SpringApplicationRunListener

详细内容看这篇博客:SpringApplicationRunListeners 监听器执

http://www.lryc.cn/news/336617.html

相关文章:

  • 雄安建博会:中矿雄安新区的总部开工建设
  • 蒙特卡洛方法【强化学习】
  • 构建第一个ArkTS之声明式UI描述
  • pytest教程-25-生成覆盖率报告插件-pytest-cov
  • 特征工程总结
  • JUC并发编程2(高并发,AQS)
  • Golang 为什么需要用反射
  • 【Linux的进程篇章 - 进程终止和进程等待的理解】
  • 《策略模式(极简c++)》
  • Python向文件里写入数据
  • 【网站项目】校园订餐小程序
  • vue-指令v-for
  • Python项目1 外星人入侵_外星人
  • 导入项目运行后,报错java: Cannot find JDK ‘XX‘ for module ‘XX‘
  • JS rgb,hex颜色值转换
  • Linux| Awk 中“next”命令奇用
  • 基于Springboot的箱包存储系统(有报告)。Javaee项目,springboot项目。
  • JavaScript_语法--变量
  • P1843 奶牛晒衣服
  • 功能强大:JMeter 常用插件全解析
  • vulhub之fastjson篇-1.2.27-rce
  • 基于springboot实现教师工作量管理系统项目【项目源码+论文说明】计算机毕业设计
  • [StartingPoint][Tier1]Crocodile
  • 【Qt】:常用控件(四:显示类控件)
  • gradio简单搭建——关键词简单筛选【2024-4-11优化】
  • docker完美安装分布式任务调度平台XXL-JOB
  • java使用while循环输出2-100的所有素数
  • VSCode中调试C++程序
  • Can Transformer and GNN Help Each Other?
  • 在隐私计算应用中和数链具备哪些技术特点?