代码语言:javascript复制
@Component
public class PropertySourcesUtil {
@Autowired
ConfigurableEnvironment environment;
public String getProperty(String key) {
MutablePropertySources propertySources = environment.getPropertySources();
for (PropertySource<?> propertySource : propertySources) {
Object property = propertySource.getProperty(key);
if (property != null) {
return Objects.toString(property);
}
}
return null;
}
}
比如有如下配置项
代码语言:javascript复制my:
name: test
url: https://${my.name}.com
那么使用该工具类getProperty("my.url")
获取到的就是 https://${my.name}.com
原文, 而不是 https://test.com