javascript
springboot中获得app_在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?
解決方案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.
This can be tested easily, 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
}
}
總結
以上是生活随笔為你收集整理的springboot中获得app_在SpringBoot中读取环境变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中位数_java 计算中位数方
- 下一篇: python函数递归法求一个数各位数之和