springboot应用服务启动事件的监听实现_第1页
springboot应用服务启动事件的监听实现_第2页
springboot应用服务启动事件的监听实现_第3页
springboot应用服务启动事件的监听实现_第4页
springboot应用服务启动事件的监听实现_第5页
已阅读5页,还剩1页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第springboot应用服务启动事件的监听实现目录一、简介二、常用场景介绍二、代码小实验通过@Component定义方式实现通过@Bean定义方式实现三、执行测试四、总结五、问题总结

一、简介

SpringBoot提供了两个接口:CommandLineRunner、ApplicationRunner,用于启动应用时做特殊处理,这些代码会在SpringApplication的run()方法运行完成之前被执行。相对于之前章节为大家介绍的Spring的ApplicationListener接口自定义监听器、Servlet的ServletContextListener监听器。使用二者的好处在于,可以方便的使用应用启动参数,根据参数不同做不同的初始化操作。

二、常用场景介绍

实现CommandLineRunner、ApplicationRunner接口。通常用于应用启动前的特殊代码执行,比如:

将系统常用的数据加载到内存应用上一次运行的垃圾数据清理系统启动成功后的通知的发送等

如下图是我实现了CommandLineRunner接口,在应用启动时将系统内常用的配置数据。从数据库加载到内存,以后使用该数据的时候只需要调用getSysConfigList方法,不需要每次使用该数据都去数据库加载。节省系统资源、缩减数据加载时间。

二、代码小实验通过@Component定义方式实现

CommandLineRunner:参数是字符串数组

@Slf4j

@Component

publicclassCommandLineStartupRunnerimplementsCommandLineRunner{

@Override

publicvoidrun(String...args){

("CommandLineRunner传入参数:{}",Arrays.toString(args));

ApplicationRunner:参数被放入ApplicationArguments,通过getOptionNames()、getOptionValues()、getSourceArgs()获取参数

@Slf4j

@Component

publicclassAppStartupRunnerimplementsApplicationRunner{

@Override

publicvoidrun(ApplicationArgumentsargs){

("ApplicationRunner参数名称:{}",args.getOptionNames());

("ApplicationRunner参数值:{}",args.getOptionValues("age"));

("ApplicationRunner参数:{}",Arrays.toString(args.getSourceArgs()));

通过@Bean定义方式实现

这种方式可以指定执行顺序,注意前两个Bean是CommandLineRunner,最后一个Bean是ApplicationRunner。

@Configuration

publicclassBeanRunner{

@Bean

@Order(1)

publicCommandLineRunnerrunner1(){

returnnewCommandLineRunner(){

@Override

publicvoidrun(String...args){

System.out.println("BeanCommandLineRunnerrun1()"+Arrays.toString(args));

@Bean

@Order(2)

publicCommandLineRunnerrunner2(){

returnnewCommandLineRunner(){

@Override

publicvoidrun(String...args){

System.out.println("BeanCommandLineRunnerrun2()"+Arrays.toString(args));

@Bean

@Order(3)

publicApplicationRunnerrunner3(){

returnnewApplicationRunner(){

@Override

publicvoidrun(ApplicationArgumentsargs){

System.out.println("BeanApplicationRunnerrun3()"+Arrays.toString(args.getSourceArgs()));

可以通过@Order设置执行顺序

三、执行测试

在IDEASpringboot启动配置中加入如下参数,保存后启动应用

测试输出结果:

c.z.boot.launch.config.AppStartupRunner:ApplicationRunner参数名称:[name,age]

c.z.boot.launch.config.AppStartupRunner:ApplicationRunner参数值:[18]

c.z.boot.launch.config.AppStartupRunner:ApplicationRunner参数:[--name=zimug,--age=18]

BeanApplicationRunnerrun3()[--name=zimug,--age=18]

c.z.b.l.config.CommandLineStartupRunner:CommandLineRunner传入参数:[--name=zimug,--age=18]

BeanCommandLineRunnerrun1()[--name=zimug,--age=18]

e=18]

BeanCommandLineRunnerrun2()[--name=zimug,--age=18]

从测试结果上看(笔者目前不敢确定这个优先级顺序是不是常态,但从我的多次测试效果,顺序一直是这样的):

ApplicationRunner执行优先级高于CommandLineRunner以Bean的形式运行的Runner优先级要低于Component注解加implementsRunner接口的方式Order注解只能保证同类的CommandLineRunner或ApplicationRunner的执行顺序,不能跨类保证顺序

四、总结

CommandLineRunner、ApplicationRunner的核心用法是一致的,就是用于应用启动前的特殊代码执行。ApplicationRunner的执行顺序先于CommandLineRunner;ApplicationRunner将参数封装成了对象,提供了获取参数名、参数值等方法,操作上会方便一些。

五、问题总结

这是笔者在实践中真实遇到的问题,就是我定义了多个CommandLineRunner的实现。出现奇怪的问题是:当你定义多个CommandLineRunner的实现的时候,其中一个或者几个将不会执行。

分析一下:下面的代码是SpringBootApplication启动项目之后会执行的代码,大家看代码中通过一个遍历来启动CommandLineRunner或者ApplicationRunner。也就是说,只有上一个CommandLineRunner执行完成之后,才会执行下一个CommandLineRunner,是同步执行的。

privatevoidcallRunners(ApplicationContextcontext,ApplicationArgumentsargs){

ListObjectrunners=newArrayList();

runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());

runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());

AnnotationAwareOrderComparator.sort(runners);

for(Objectrunner:newLinkedHashSet(runners)){

if(runnerinstanceofApplicationRunner){

callRunner((ApplicationRunner)runner,args);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论