Ads by ProfitSence
Close

Spring Boot Get Environment Variable

Last Updated on Wednesday 5th Oct 2022

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

			
					@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);
    }
}

			
	

References