基于命令行参数的多个Spring Boot CommandLineRunner [英] Multiple Spring boot CommandLineRunner based on command line argument

查看:436
本文介绍了基于命令行参数的多个Spring Boot CommandLineRunner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用spring cloud任务创建了spring boot应用程序,该任务应该执行一些命令(任务). 每个任务/命令都是短期任务,所有任务都从命令行开始,执行一些简短的ETL作业并完成执行.

I have created spring boot application with spring cloud task which should executes a few commands(tasks). Each task/command is shorted-lived task, and all tasks are start from command line, do some short ETL job and finish execution.

有一个包含所有命令/任务的spring boot jar. 每个任务都是CommandLineRunner,我想根据命令行中的参数来决定执行哪些任务(一个或多个). 最佳做法是什么? 我不喜欢有肮脏的代码问如果不是"或类似的东西.

There is one spring boot jar which contain all the commands/tasks. Each task is CommandLineRunner, and I like to decide which tasks (one or more) will be executed based on the params from command line. What is the best practice to do so? I don't like to have dirty code which ask "if else" or something like this.

推荐答案

Spring Boot从应用程序上下文运行所有CommandLineRunnerApplicationRunner Bean.您无法通过任何参数选择一个.

Spring Boot runs all the CommandLineRunner or ApplicationRunner beans from the application context. You cannot select one by any args.

所以基本上您有两种可能性:

So basically you have two possibiities:

  1. 您有不同的CommandLineRunner实现,并且在每个实现中都要检查参数以确定是否应运行特殊的CommandLineRunner.
  2. 您仅实现一个充当调度程序的CommandLineRunner.代码可能看起来像这样:
  1. You have different CommandLineRunner implementations and in each you check the arguments to determine if this special CommandLineRunner should run.
  2. You implement only one CommandLineRunner which acts as a dispatcher. Code might look something like this:

这是跑步者将实现的新界面:

This is the new Interface that your runners will implement:

public interface MyCommandLineRunner {
    void run(String... strings) throws Exception;
}

然后定义实现并用名称标识它们:

You then define implementations and identify them with a name:

@Component("one")
public class MyCommandLineRunnerOne implements MyCommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(MyCommandLineRunnerOne.class);

    @Override
    public void run(String... strings) throws Exception {
        log.info("running");
    }
}

@Component("two")
public class MyCommandLineRunnerTwo implements MyCommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(MyCommandLineRunnerTwo.class);
    @Override
    public void run(String... strings) throws Exception {
        log.info("running");
    }
}

然后在单个CommandLineRunner实现中,您将掌握应用程序上下文并按名称解析所需的bean,我的示例仅使用第一个参数,并将其称为MyCommandLineRunner.run()方法:

Then in your single CommandLineRunner implementation you get hold of the application context and resolve the required bean by name, my example uses just the first argument, and call it's MyCommandLineRunner.run()method:

@Component
public class CommandLineRunnerImpl implements CommandLineRunner, ApplicationContextAware {
    private ApplicationContext applicationContext;


    @Override
    public void run(String... strings) throws Exception {
        if (strings.length < 1) {
            throw new IllegalArgumentException("no args given");
        }

        String name = strings[0];
        final MyCommandLineRunner myCommandLineRunner = applicationContext.getBean(name, MyCommandLineRunner.class);
        myCommandLineRunner.run(strings);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

这篇关于基于命令行参数的多个Spring Boot CommandLineRunner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆