解读CommandLineRunner或者ApplicationRunner接口

 更新时间:2023年02月13日 10:06:47   作者:二月_春风  
这篇文章主要介绍了解读CommandLineRunner或者ApplicationRunner接口的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

前言

CommandLineRunner、ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。

CommandLineRunner接口

CommandLineRunner

官方doc:

Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or Order @Order annotation.

接口被用作将其加入spring容器中时执行其run方法。多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。

If you need access to ApplicationArguments instead of the raw String array
consider using ApplicationRunner.

如果你需要访问ApplicationArguments去替换掉字符串数组,可以考虑使用ApplicationRunner类。

先看一个demo:

定义一个ServerStartedReport实现CommandLineRunner,并纳入到srping容器中进行处理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Component
public class ServerStartedReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now());
    }
}

定义一个ServerSuccessReport实现CommandLineRunner,并纳入到spring容器处理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
 
@Order(1)
@Component
public class ServerSuccessReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("=====应用已经成功启动====="+ Arrays.asList(args));
    }
}

启动类测试,也可以直接在spring容器访问该值,

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
 
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);
        ApplicationArguments applicationArguments = context.getBean(ApplicationArguments.class);
        System.out.println("============");
        System.out.println("name="+applicationArguments.getOptionNames());
        System.out.println("values===="+applicationArguments.getOptionValues("developer.name"));
    }
}

配置参数,然后执行启动类

打印结果

ApplicationRunner接口

发现二者的官方javadoc一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理。ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

ApplicationArguments是对参数(main方法)做了进一步的处理,可以解析--name=value的,我们就可以通过name来获取value(而CommandLineRunner只是获取--name=value)

可以接收--foo=bar这样的参数。

  • --getOptionNames()方法可以得到foo这样的key的集合。
  • --getOptionValues(String name)方法可以得到bar这样的集合的value。

看一个demo:

定义MyApplicationRunner类继承ApplicationRunner接口:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
 
@Component
public class MyApplicationRunner implements ApplicationRunner{
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));
        System.out.println("===getOptionNames========"+args.getOptionNames());
        System.out.println("===getOptionValues======="+args.getOptionValues("foo"));
        System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));
    }
}

启动类:

import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

配置参数启动:

打印结果:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • ElasticSearch6.2.3+head插件安装的方法步骤

    ElasticSearch6.2.3+head插件安装的方法步骤

    这篇文章主要介绍了ElasticSearch6.2.3+head插件安装的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02
  •  java中Thread.sleep()的具体使用

     java中Thread.sleep()的具体使用

    本文主要介绍了 java中Thread.sleep()的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Java线程池中的工作线程Worker类源码解析

    Java线程池中的工作线程Worker类源码解析

    这篇文章主要介绍了Java线程池中的工作线程Worker类源码解析,线程池中的工作线程是通过内部类Worker表示的,Worker继承自AbstractQueueSynchronizer,可以实现同步器的功能,需要的朋友可以参考下
    2023-12-12
  • 如何解决springcloud feign 首次调用100%失败的问题

    如何解决springcloud feign 首次调用100%失败的问题

    这篇文章主要介绍了如何解决springcloud feign 首次调用100%失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • SpringData JPA Mongodb查询部分字段问题

    SpringData JPA Mongodb查询部分字段问题

    这篇文章主要介绍了SpringData JPA Mongodb查询部分字段问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • SpringBoot单元测试没有执行的按钮问题及解决

    SpringBoot单元测试没有执行的按钮问题及解决

    这篇文章主要介绍了SpringBoot单元测试没有执行的按钮问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Aspectj框架实战案例详解

    Aspectj框架实战案例详解

    这篇文章主要介绍了Aspectj框架实战,结合具体案例形式详细分析了Aspectj框架具体配置、使用、编译等相关操作技巧,需要的朋友可以参考下
    2020-01-01
  • PowerJob的CleanService清理服务流程

    PowerJob的CleanService清理服务流程

    这篇文章主要为大家介绍了PowerJob的CleanService清理服务流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪<BR>
    2024-02-02
  • Java编程实现的二维数组转置功能示例

    Java编程实现的二维数组转置功能示例

    这篇文章主要介绍了Java编程实现的二维数组转置功能,结合实例形式分析了Java二维数组的遍历、运算、赋值等实现转置的相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • SpringMVC中Model与Session的区别说明

    SpringMVC中Model与Session的区别说明

    这篇文章主要介绍了SpringMVC中Model与Session的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12

最新评论