java Spring Boot CommandLineRunner:过滤器选项参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27059936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:07:53  来源:igfitidea点击:

Spring Boot CommandLineRunner : filter option argument

javaspringcommand-linespring-batchspring-boot

提问by JR Utily

Considering a Spring Boot CommandLineRunnerApplication, I would like to know how to filter the "switch" options passed to Spring Boot as externalized configuration.

考虑到 Spring BootCommandLineRunner应用程序,我想知道如何过滤作为外部化配置传递给 Spring Boot 的“开关”选项。

For example, with:

例如,与:

@Component
public class FileProcessingCommandLine implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        for (String filename: strings) {
           File file = new File(filename);
           service.doSomething(file);
        }
    }
}

I can call java -jar myJar.jar /tmp/file1 /tmp/file2and the service will be called for both files.

我可以打电话java -jar myJar.jar /tmp/file1 /tmp/file2,两个文件都会调用该服务。

But if I add a Spring parameter, like java -jar myJar.jar /tmp/file1 /tmp/file2 --spring.config.name=myprojectthen the configuration name is updated (right!) but the service is also called for file ./--spring.config.name=myprojectwhich of course doesn't exist.

但是如果我添加一个 Spring 参数,java -jar myJar.jar /tmp/file1 /tmp/file2 --spring.config.name=myproject那么配置名称就会更新(对!)但是该服务也被称为文件./--spring.config.name=myproject,这当然不存在。

I know I can filter manually on the filename with something like

我知道我可以使用类似的文件名手动过滤

if (!filename.startsWith("--")) ...

But as all of this components came from Spring, I wonder if there is not a option somewhere to let it manage it, and to ensure the stringsparameter passed to the runmethod will not contain at all the properties options already parsed at the Application level.

但是由于所有这些组件都来自 Spring,我想知道是否没有选项可以让它管理它,并确保strings传递给run方法的参数根本不包含已在应用程序级别解析的所有属性选项。

采纳答案by JR Utily

Thanks to @AndyWilkinson enhancement report, ApplicationRunnerinterface was added in Spring Boot 1.3.0 (still in Milestones at the moment, but will soon be released I hope)

感谢@AndyWilkinson 增强报告,ApplicationRunnerSpring Boot 1.3.0 中添加了接口(目前仍在 Milestones 中,但我希望很快会发布)

Here the way to use it and solve the issue:

这是使用它并解决问题的方法:

@Component
public class FileProcessingCommandLine implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        for (String filename : applicationArguments.getNonOptionArgs()) 
           File file = new File(filename);
           service.doSomething(file);
        }
    }
}

回答by Andy Wilkinson

There's no support for this in Spring Boot at the moment. I've opened an enhancement issueso that we can consider it for a future release.

目前 Spring Boot 不支持此功能。我已经打开了一个增强问题,以便我们可以在未来的版本中考虑它。

回答by Arun Avanathan

One option is to use Commons CLIin the run() of your CommandLineRunner impl.

一种选择是在 CommandLineRunner impl 的 run() 中使用Commons CLI

There is a related questionthat you may be interested.

有一个您可能感兴趣的相关问题

回答by Arun Avanathan

Here is another solution :

这是另一个解决方案:

@Component
public class FileProcessingCommandLine implements CommandLineRunner {

    @Autowired
    private ApplicationConfig config;

    @Override
    public void run(String... strings) throws Exception {

        for (String filename: config.getFiles()) {
           File file = new File(filename);
           service.doSomething(file);
        }
    }
}


@Configuration
@EnableConfigurationProperties
public class ApplicationConfig {
    private String[] files;

    public String[] getFiles() {
        return files;
    }

    public void setFiles(String[] files) {
        this.files = files;
    }
}

Then run the program :

然后运行程序:

java -jar myJar.jar --files=/tmp/file1,/tmp/file2 --spring.config.name=myproject