Evgeny Makarov
Evgeny Makarov

Reputation: 1448

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

Upvotes: 1

Views: 3667

Answers (2)

Sourabh Kanojiya
Sourabh Kanojiya

Reputation: 161

you can run your app like this:

$ java -server -Dmyproperty=blabla -jar myapp.jar

and can access the value of myproperty in code.

Upvotes: 0

jny
jny

Reputation: 8067

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.

Upvotes: 1

Related Questions