Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot CommandLineRunner : filter option argument

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

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/file2 and the service will be called for both files.

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("--")) ...

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.

like image 698
JR Utily Avatar asked Nov 21 '14 11:11

JR Utily


People also ask

What is the use of CommandLineRunner in spring boot?

CommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.

What is the difference between application runner and command line runner?

The difference between CommandLineRunner and ApplicationRunner is that the run() method of CommandLineRunner accepts array of String as an argument and run() method of ApplicationRunner accepts spring ApplicationArguments as an argument.

Which function is present in CommandLineRunner interface in spring?

Interface CommandLineRunner Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. Interface used to indicate that a bean should run when it is contained within a SpringApplication .


3 Answers

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);
        }
    }
}
like image 84
JR Utily Avatar answered Oct 19 '22 03:10

JR Utily


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

like image 44
Andy Wilkinson Avatar answered Oct 19 '22 04:10

Andy Wilkinson


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
like image 43
Arun Avanathan Avatar answered Oct 19 '22 04:10

Arun Avanathan