SpringBoot获取配置项原内容

2023-02-23 22:36:05 浏览数 (1)

代码语言: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

0 人点赞