首页 > 解决方案 > 如何在春季重用.conf文件中的配置值

问题描述

conf在我的 spring 项目中使用文件作为配置文件。在该文件下,值定义为

dev {
  notification {
    cms {
      endpointPrefix: "https://example.com/notification"
    }
  }
  cms {
    url: "https://example.com"
    service {
      url: "https://example.com/hello"
      timeout: 5000
    }
  }
  appConfig {
    url: "https://example.com/values/app"
    refreshFrequencyInMins: 30
  }
}

现在我想https://example.com用一些共同的值替换所有的用法,这样我就不需要多次定义同一个域,而且每当我需要更新它时,我几乎不需要改变。我确实尝试过这样

dev {
  cms_url: "https://example.com"
  notification {
    cms {
      endpointPrefix: "${cms_url}/notification"
    }
  }
  cms {
    url: "${cms_url}"
    service {
      url: "${cms_url}/hello"
      timeout: 5000
    }
  }
  appConfig {
    url: "${cms_url}/values/app"
    refreshFrequencyInMins: 30
  }
}

但它不起作用,所以想了解几件事

  1. 我需要用 替换cms_urldev.cms_url
  2. conf文件中的变量替换如何工作?
  3. 它是从根路径还是从相对路径检查变量,然后转到上层,直到变量不存在?
  4. notification是之前定义的,cms所以如果我想在中使用“cms.url” notification.cms.endpointPrefix,它会起作用还是顺序问题?

标签: javaspringspring-bootconfigapp-config

解决方案


推荐阅读