23.7 Accessing application arguments
If you need to access the application arguments that were passed to SpringApplication.run(…)
you can inject a org.springframework.boot.ApplicationArguments
bean. The ApplicationArguments
interface provides access to both the raw String[]
arguments as well as parsed option
and non-option
arguments:
import org.springframework.boot.* import org.springframework.beans.factory.annotation.* import org.springframework.stereotype.* _@Component_ public class MyBean { _@Autowired_ public MyBean(ApplicationArguments args) { boolean debug = args.containsOption("debug"); List<String> files = args.getNonOptionArgs(); // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"] } }
Tip | |
---|---|
Spring Boot will also register a CommandLinePropertySource with the Spring Environment . This allows you to also inject single application arguments using the @Value annotation. |