Read environment variable in SpringBoot

We Are Going To Discuss About Read environment variable in SpringBoot. So lets Start this Java Article.

Read environment variable in SpringBoot

  1. Read environment variable in SpringBoot

    Here are three “placeholder” syntaxes that work for accessing a system environment variable named MY_SECRET:
    @Value("${MY_SECRET:aDefaultValue}") private String s1;

  2. Read environment variable in SpringBoot

    Here are three “placeholder” syntaxes that work for accessing a system environment variable named MY_SECRET:
    @Value("${MY_SECRET:aDefaultValue}") private String s1;

Solution 1

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
    }
}

Original Author g00glen00b Of This Content

Solution 2

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;

Original Author Rlarroque Of This Content

Solution 3

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?

Original Author Brent Bradburn Of This Content

Solution 4

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…

Original Author Saikat Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment