Skip to content

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:

spring boot arguments

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:

MyCommand.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("VM option k1's value is {}", System.getProperty("k1")); // The key point is to use System.getProperty(key)
}
}

Run the code, and you will get:

Terminal window
2019-05-22 22:51:50.434 INFO 27066 --- [ main] MyCommand : VM option k1's value is v1

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.

MyCommand.java
package com.test.sb1jt.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Value("${k1}")
private String k1;
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("auto config k1 value is {}", k1);
}
}

Run the code, and you will get:

Terminal window
2019-05-22 22:57:27.035 INFO 27086 --- [ main] MyCommand : auto config k1 value is v1

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!