Search code examples
spring-bootspring-batchspring-environment

Spring boot get command line args in configuration code


I got Spring boot with Spring batch project. Inside my BatchConfiguration I want to get command line arguments so I @Autowire Environment object and try to get my prop but I'm getting null.

After some debug I figured out that I can get all command line args via special property Name "nonOptionArgs" but in this case I got plain string of all arguments passed. Is there some better solution?

Thanks


Solution

  • You are doing everything right with autowired environment. Make sure you pass arguments in the command line with "--"

    From documentation:

    By default SpringApplication will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Spring Environment. As mentioned above, command line properties always take precedence over other property sources.