Two simple ways to get System properties or VM options in SpringBoot apps
1. The purpose of this post
I will demonstrate two simple ways to retrieve System properties or VM options in SpringBoot applications.
2. Environments
- SpringBoot 1.x and 2.x
3. Two simple ways to get System properties (VM options) from SpringBoot apps
3.1 The question
According to the previous article, when we pass VM arguments in IntelliJ IDEA to SpringBoot applications like this:
We cannot retrieve the VM options values using CommandLineRunner
or ApplicationRunner
. So, how can we retrieve VM options in SpringBoot applications?
3.2 Way 1: Use the System.getProperty
Since VM options are stored in JVM system properties, you can use System.getProperty
to retrieve them:
Run the code, and you will get:
3.3 Way 2: Use the @Value annotation
The @Value
annotation can automatically parse VM options as class property values.
Annotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument. Typically used for expression-driven dependency injection. Also supported for dynamic resolution of handler method parameters, e.g., in Spring MVC. A common use case is to assign default field values using
#{systemProperties.myProp}
style expressions.
Run the code, and you will get:
4. Conclusion
You can use either System.getProperty
or the @Value
annotation to retrieve VM options in SpringBoot applications. Both methods are effective, but @Value
provides a more integrated approach within the Spring ecosystem.
5. Summary
In this post, we explored two methods for retrieving VM options in SpringBoot applications. The first method uses System.getProperty
, which directly accesses JVM system properties. The second method leverages the @Value
annotation to inject VM options into class fields automatically. Both approaches are straightforward and can be used depending on your specific requirements. By understanding these techniques, you can better manage and utilize VM options in your SpringBoot projects.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!