springboot 接收对象数组_SpringBoot之Runner启动器

一般项目在部署启动后,需要执行一些诸如清除缓存等初始化的工作,虽然可以通过人工手动调用接口等方式完成,但是会容易遗漏,且不够优雅。这里推荐使用SpringBoot的Runner启动器,其会在服务启动后自动地执行相关初始化任务

CommandLineRunner、ApplicationRunner接口

SpringBoot提供了两个Runner启动器——CommandLineRunner、ApplicationRunner接口。二者实际上并无太大区别,只是前者是通过数组接收启动参数而后者则是将启动参数封装到ApplicationArguments对象中。实际开发过程中只需在接口的run方法中实现我们的初始化操作即可。当然不要忘了在启动器类上添加@Component注解

@FunctionalInterface
public interface CommandLineRunner {
    /**
     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    void run(String... args) throws Exception;
}
...
@FunctionalInterface
public interface ApplicationRunner {
    /**
     * Callback used to run the bean.
     * @param args incoming application arguments
     * @throws Exception on error
     */
    void run(ApplicationArguments args) throws Exception;
}

实践

我们以项目启动后打印相关信息为例介绍其用法,这里我们有3个Runner启动器类。当有多个Runner启动器时,可通过@Order注解指定执行的优先级顺序,数值越小,优先级越高,越先被执行

@Component
@Order(1)
public class SystemPrompt1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("SystemPrompt1: 系统初始化完成");

        // 获取参数 方式1
        System.out.println("------- 获取参数 方式1 -------");
        for(String str : args) {
            System.out.println("str: " + str);
        }
    }
}
...
@Component
@Order(value = 2)
public class SystemPrompt2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("SystemPrompt2: 系统初始化完成");
    }
}
...
@Component
@Order(value = 3)
public class SystemPrompt3 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("SystemPrompt3: 系统初始化完成");

        // 获取参数 方式1
        System.out.println("------- 获取参数 方式1 -------");
        for(String str : args.getSourceArgs()) {
            System.out.println("str: " + str);
        }

        // 获取参数 方式2
        System.out.println("------- 获取参数 方式2 -------");
        for(String str : args.getOptionNames()) {
            System.out.println("key: " + str + " value: " + args.getOptionValues(str));
        }
    }
}

然后,我们在 IntelliJ IDEA 中添加项目的启动参数

f47f49847c61535fa2bec5459db52ffd.png

启动后测试结果如下所示,Runner启动器相互之间的顺序符合我们的预期

605cd5c584c925735446502bade7e8d8.png

在ApplicationRunner中,main参数即可通过getSourceArgs()来获取原始的字符串数组,也可以通过getOptionNames()、getOptionValues(String name)方法分别获取参数的名称集合、指定名称参数的值(值的类型为List<String>)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值