如何在属性文件中使用groovy形式参数

如何在属性文件中使用groovy形式参数,groovy,properties,Groovy,Properties,我有这样一个属性文件: envHost=http://some.host api.host=${envHost} backoffice.host=${envHost} InputStream input = new FileInputStream(new File(getClass().getResource("/envs/someEnv.properties").toURI())) Properties prop = new Propert

我有这样一个属性文件:

envHost=http://some.host
api.host=${envHost}
backoffice.host=${envHost}
        InputStream input = new FileInputStream(new File(getClass().getResource("/envs/someEnv.properties").toURI()))
        Properties prop = new Properties();
        prop.load(input)
        println prop.'api.host'
当我从属性文件中获取api.host属性时:

envHost=http://some.host
api.host=${envHost}
backoffice.host=${envHost}
        InputStream input = new FileInputStream(new File(getClass().getResource("/envs/someEnv.properties").toURI()))
        Properties prop = new Properties();
        prop.load(input)
        println prop.'api.host'
我得到的是
${envHost}
而不是
http://some.host


如何获取值
http://some.host
从属性文件中使用正式参数?

如果您可以将属性文件更改为配置slurper有效格式:

envHost="http://some.host"
api.host=envHost
backoffice.host=envHost
然后,您可以执行以下操作:

def cfg = new ConfigSlurper().parse(getClass().getResource("/envs/someEnv.properties"))
println cfg.api.host

而且它应该工作

我如何才能获得财产价值?ConfigObject上的get()或getProperty()返回null或[:]假设类路径中存在
“/envs/someEnv.properties”
,则只需执行点属性(请参见上文,我更改了示例)是的,这很有效,谢谢!如果我们的属性键像一个变量呢?e、 g.我们有字符串propertyKeyName='api.host'然后是cfg。“${propertyKeyName}”不起作用,它返回[:]Ahhh,然后需要拆分键并遍历值。使用Groovy3可以执行以下操作:
propertyNameKey.split('\\..).injection(cfg){m,k->m?[k]}
Superb!一切都很好,非常感谢!!