使用Groovy在Java属性中进行变量扩展 [英] Using Groovy for variable expansion in Java properties
问题描述
我经常使用标准的Java属性文件来配置我的Groovy应用程序。我一直缺少的一个功能是能够将变量作为属性值的一部分,以便在使用过程中可以动态扩展它们。我认为我可以使用以下设计提供此功能:
- 使用特殊格式注释应展开的属性。我已选择将这些模板放在双重感叹号(!!)中。这些属性值本质上是一个要用局部变量扩展的模板
- 在应用程序中使用属性之前,使用groovy'evaluate'方法来扩展模板中的应用程序变量。 >
- 在使用前将原始属性键重新分配给新值
所以,如果我有属性文件 config.properties ,其属性如下:
version = 2.3
local_lib =! !$ {ENV [ 'GROOVY_HOME']} / $ {configProps.getProperty( '版本')} / lib目录!
local_lib 属性将从 GROOVY_HOME em>环境变量和版本属性值。
在我的应用程序中,我编码如下:
//加载环境变量和配置文件
env = System.getenv()
configFile =新的File('config.properties')
configProps = new Properties()
configProps.load(configFile.newDataInputStream())
//替换配置属性值等价
configProps.each {
//如果一个属性值是一个模板我们评估它
if(it.value.startsWith('!!')){
valTemplate = it.value.replace('!!',''')
it.value = evaluate(valTemplate)
}
}
//然后我们使用展开后的属性值
这似乎起作用了,当我做时
prin tln configProps
我看到这个值被扩展了,而不是空的
然而,扩展属性的 getProperty 方法返回null。
assert configProps.getProperty('local_lib')=='C:\\DEVTOOLS\\groovy-2.4.7 / 2.3 / lib'
| | |
| null false
[local_lib:C:\DEVTOOLS\groovy-2.4.7 / 2.3 / lib,版本:2.3]
什么导致了这种差异?我希望能够返回属性映射中显示的值。
您的 local_lib
value看起来像是一个 String
,但它不是。这是一个 GString
,根据需要只是懒惰地强制转换为 String
(就像打印出 configProps
map value)。
因此,一个小的已知效果 Properties.getProperty()
在这里生效。当实际映射值不是字符串时, Properties.getProperty()
返回 null
。
因此,为了获得所需的行为,您需要强制 GString
为 String
,然后将值存储在属性图中。像这样:
it.value = evaluate(valTemplate).toString()
或
it.value = evaluate(valTemplate )as String
然后您应该在下游看到所需的结果。
I frequently use standard Java property files for configuring my Groovy applications. One feature I have been missing is the ability to use variables as part of the property value so they can be expand dynamically during use. I thought I could provide this functionality using the following design:
- Use a special format to annotate the properties that should be expanded. I have chosen to enclose such templates in double exclamation marks (!!). These property values are essentially a template to be expanded with the local variables
- Before using the properties in the application, use the groovy 'evaluate' method to expand application variables in the template
- Re-assign the original property key to the new value before use
So, if I have a property file config.properties with properties like:
version=2.3
local_lib=!!${env['GROOVY_HOME']}/${configProps.getProperty('version')}/lib!!
The local_lib property will be expanded from the GROOVY_HOME environment variable and the version property value.
In my application, I have coded this as follows:
//Load the environment variables and configuration file
env=System.getenv()
configFile=new File('config.properties')
configProps= new Properties()
configProps.load(configFile.newDataInputStream())
//Replace configuration property values with their expanded equivalent
configProps.each{
//if a property value is a template we evaluate it
if (it.value.startsWith('!!')){
valTemplate=it.value.replace('!!','"')
it.value=evaluate(valTemplate)
}
}
//then we use the expanded property values
This seems to work. When I do
println configProps
I see that the value is expanded and not null
However, the getProperty method for the expanded property returns null.
assert configProps.getProperty('local_lib')=='C:\\DEVTOOLS\\groovy-2.4.7/2.3/lib'
| | |
| null false
[local_lib:C:\DEVTOOLS\groovy-2.4.7/2.3/lib, version:2.3]
What is causing this discrepancy? I would have expected to return the value shown in the property map.
Your local_lib
value looks like a String
, but it isn't. It is a GString
, only lazily coerced to String
as needed (like when printing out the configProps
map value).
Thus, a little known effect of Properties.getProperty()
takes effect here. When the actual map value is not a String, Properties.getProperty()
returns null
.
So, in order to get the desired behavior, you need to coerce the GString
to String
before you store the value in the property map. Like so:
it.value=evaluate(valTemplate).toString()
or
it.value=evaluate(valTemplate) as String
Then you should see the desired results downstream.
这篇关于使用Groovy在Java属性中进行变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!