前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot获取配置项原内容

SpringBoot获取配置项原内容

原创
作者头像
code-x
发布2023-02-23 22:36:05
9100
发布2023-02-23 22:36:05
举报
文章被收录于专栏:code-xcode-x
代码语言: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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档