Read Environment Variable in Springboot

What is the best way to read environment variables in SpringBoot?
In Java I did it using:

String foo = System.getenv("bar");

Is it possible to do it using @Value annotation?

0

8 Answers

Quoting the documentation:

Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.

So, since Spring boot allows you to use environment variables for configuration, and since Spring boot also allows you to use @Value to read a property from the configuration, the answer is yes.


For example, the following will give the same result:

@Component
public class TestRunner implements CommandLineRunner {
    @Value("${bar}")
    private String bar;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void run(String... strings) throws Exception {
        logger.info("Foo from @Value: {}", bar);
        logger.info("Foo from System.getenv(): {}", System.getenv("bar")); // Same output as line above
    }
}

You can do it with the @Value annotation:

@Value("${bar}")
private String myVariable;

You can also use colon to give a default value if not found:

@Value("${bar:default_value}")
private String myVariable;
1

Here are three "placeholder" syntaxes that work for accessing a system environment variable named MY_SECRET:

@Value("${MY_SECRET:aDefaultValue}")
private String s1;

@Value("#{environment.MY_SECRET}")
private String s2;

@Value("${myApp.mySecretIndirect:aDefaultValue}") // via application property
private String s3;

In the third case, the placeholder references an application property that has been initialized from the system environment in a properties file:

myApp.mySecretIndirect=${MY_SECRET:aDefaultValue}

For @Value to work, it must be used inside a live @Component (or similar). There are extra gochas if you want this to work during unit testing -- see my answer to Why is my Spring @Autowired field null?

3

Alternatively, you can use the org.springframework.core.env.Environment interface to access environment variables:

import org.springframework.core.env.Environment;

@Autowired
private Environment env;

//...

System.out.println(env.getProperty("bar"));

Read more...

1

Yes, you can. However, most answer didn't mention, the ordering is very important, please check this

Your OS environment variables will overwrite the value come from Application properties packaged inside your jar (application.properties and YAML variants)., so basically, your environment variables has higher priority.

You can place your environment variable in an file and then you can fetch the value using the @Value annotation. But in order to use @Value annotation your class should be a bean and should be annotated with @Component annnotation. You can also provide a default value for the variable.

@Component
@NoArgsConstructor
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MyClass {
 
@Value("${something.variable:<default-value>}")
private String myEnvVariable;

}
    

you can use it with The @Value annotation for the @Components and @service class Some times it won't work if it is a normal class

Example:

@Component
public class Testclass{
    @Value("${MY_SECRET:aDefaultValue}")
    private String test1;

    @Value("#{environment.MY_SECRET}")
    private String test1;

    @Value("${myApp.mySecretIndirect:aDefaultValue}")
    private String test1;

    //to get the properties list whih are in "," seperated
    @Value("${my.list.of.strings}")
    private List<String> myList;
}

First, you have to define the relevant field information in the properties configuration file, and then use @ value to obtain and use example:

@Value("${abc}")
private String abc;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest