解读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);
    }
}

配置参数启动:

打印结果:

总结

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

相关文章

  • 对比Java中的Comparable排序接口和Comparator比较器接口

    对比Java中的Comparable排序接口和Comparator比较器接口

    Comparable和Comparator接口都可用作普通意义上对象间的比大小,但两个接口在实例化方面的用法不尽相同,接下来我们就来详细对比Java中的Comparable排序接口和Comparator比较器接口
    2016-05-05
  • 简单讲解在Java编程中实现设计模式中的单例模式结构

    简单讲解在Java编程中实现设计模式中的单例模式结构

    这篇文章主要介绍了简单讲解在Java编程中实现设计模式中的单例模式结构,设计模式是最基本直白简单的一种设计模式,需要的朋友可以参考下
    2016-04-04
  • SpringMVC项目访问controller时候报404的解决

    SpringMVC项目访问controller时候报404的解决

    这篇文章主要介绍了SpringMVC项目访问controller时候报404的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java编程接口回调一般用法代码解析

    Java编程接口回调一般用法代码解析

    本文的主要内容是同过实际代码向大家展示Java编程中接口回调的一般用法,具有一定参考价值,需要的朋友可以了解下
    2017-09-09
  • IntelliJ IDEA像Eclipse一样打开多个项目的图文教程

    IntelliJ IDEA像Eclipse一样打开多个项目的图文教程

    这篇文章主要介绍了IntelliJ IDEA像Eclipse一样打开多个项目的方法图文教程讲解,需要的朋友可以参考下
    2018-03-03
  • Java使用Ajax异步上传文件

    Java使用Ajax异步上传文件

    使用Ajax上传文件的应用场景颇多,比如上传用户头像、博客文章中插入图片、对认证用户相关身份进行校验等等很多很多。本文提供一个简单的示例供大家参考
    2021-05-05
  • java Spring整合Freemarker的详细步骤

    java Spring整合Freemarker的详细步骤

    本文对Spring整合Freemarker步骤做了详细的说明,按步骤操作一定可以整合通过,这里提供给大家做参考
    2013-11-11
  • Java中静态代理的使用与原理详解

    Java中静态代理的使用与原理详解

    这篇文章主要介绍了Java中静态代理的使用与原理详解,代理模式是为一个对象提供一个替身,以控制对这个对象的访问,即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能,需要的朋友可以参考下
    2023-09-09
  • SpringBoot利用jpa连接MySQL数据库的方法

    SpringBoot利用jpa连接MySQL数据库的方法

    这篇文章主要介绍了SpringBoot利用jpa连接MySQL数据库的方法,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • CentOS7和8中安装Maven3.8.4的简单步骤

    CentOS7和8中安装Maven3.8.4的简单步骤

    maven是属于apache的一个工具,主要是对java进行编译打包,解决依赖关系,下面这篇文章主要给大家介绍了关于CentOS7和8中安装Maven3.8.4的相关资料,需要的朋友可以参考下
    2022-04-04

最新评论