SpringBoot获取配置项原内容

kyaa111 1年前 ⋅ 429 阅读
@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;
    }
}

比如有如下配置项

my: 
  name: test
  url: https://${my.name}.com

那么使用该工具类getProperty("my.url")获取到的就是 https://${my.name}.com 原文, 而不是 https://test.com