Spring Boot CommandLineRunner:filter选项参数 [英] Spring Boot CommandLineRunner : filter option argument
问题描述
考虑到Spring Boot CommandLineRunner
应用程序,我想知道如何过滤作为外部化配置传递给Spring Boot的switch选项。
Considering a Spring Boot CommandLineRunner
Application, I would like to know how to filter the "switch" options passed to Spring Boot as externalized configuration.
例如:
@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);
}
}
}
$ c> java -jar myJar.jar / tmp / file1 / tmp / file2 ,并且将为这两个文件调用服务。
I can call java -jar myJar.jar /tmp/file1 /tmp/file2
and the service will be called for both files.
但是如果我添加一个Spring参数,如 java -jar myJar.jar / tmp / file1 / tmp / file2 --spring.config.name = myproject
(right!),但是服务也被调用为文件 ./-- spring.config.name = myproject
,当然不存在。
But if I add a Spring parameter, like java -jar myJar.jar /tmp/file1 /tmp/file2 --spring.config.name=myproject
then the configuration name is updated (right!) but the service is also called for file ./--spring.config.name=myproject
which of course doesn't exist.
我知道我可以手动过滤文件名,像
I know I can filter manually on the filename with something like
if (!filename.startsWith("--")) ...
但是由于所有这些组件都来自Spring,我想知道是否有一个选项让它管理它,并确保 strings
参数传递给运行
方法将不会包含已在应用程序级别解析的所有属性选项。
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 strings
parameter passed to the run
method will not contain at all the properties options already parsed at the Application level.
推荐答案
感谢@AndyWilkinson增强报告, code> ApplicationRunner 接口在Spring Boot 1.3.0中添加了(目前仍然在里程碑,但很快就会发布我希望)
Thanks to @AndyWilkinson enhancement report, ApplicationRunner
interface was added in Spring Boot 1.3.0 (still in Milestones at the moment, but will soon be released I hope)
这里的使用方法和解决问题:
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);
}
}
}
这篇关于Spring Boot CommandLineRunner:filter选项参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!