Spring Boot Get Environment Variable

Last Updated On Sunday 7th Nov 2021

spring boot get environment variable

As Per 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.

spring get environment variable

It allows you to use @Value to read a property from the configuration.

@Value annotation

public class TestRunner implements CommandLineRunner {
    private String bar;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    public void run(String... strings) throws Exception {
        logger.info("Foo from @Value: {}", bar);