Spring Boot CommandLineRunner:过滤器选项参数

JR *_*ily 6 java spring command-line spring-batch spring-boot

考虑到Spring Boot CommandLineRunner应用程序,我想知道如何过滤传递给Spring Boot的"switch"选项作为外部化配置.

例如,用:

@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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

我知道我可以手动过滤文件名,例如

if (!filename.startsWith("--")) ...
Run Code Online (Sandbox Code Playgroud)

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

JR *_*ily 6

感谢@AndyWilkinson增强报告,ApplicationRunner在Spring Boot 1.3.0中添加了界面(目前仍在里程碑中,但我希望很快就会发布)

在这里使用它并解决问题的方式:

@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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)