简体   繁体   English

Spring 从 applicationContext 获取值

[英]Spring get value from applicationContext

I'd like to get the value of @Value("${myVar}") from ApplicationContext .我想从ApplicationContext获取@Value("${myVar}")的值。 How to do it?怎么做? Using Spring 5.3.14.使用 Spring 5.3.14。

You can obtain the value of a property using the Environment API:您可以使用环境 API 获取属性的值:

@Autowired
private Environment env;
...
var x = env.getProperty("myVar");

You can also get the Environment from ConfigurableApplicationContext .您还可以从ConfigurableApplicationContext获取Environment https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ConfigurableApplicationContext.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ConfigurableApplicationContext.ZFC35FDC70D5FC69D2639883A822C7A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM