1

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

Evgeny Makarov
  • 1,417
  • 1
  • 21
  • 41

2 Answers2

1

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.

jny
  • 8,007
  • 3
  • 37
  • 56
0

you can run your app like this:

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

and can access the value of myproperty in code.