【发布时间】:2017-07-01 22:18:05
【问题描述】:
我对 Groovy 在这里的行为感到困惑。我已经通过调试器运行了一些尝试确定这些代码路径可能在动态混合中的哪个位置交叉但想知道是否有人可以直接在这里设置我。
基本上,在使用 GString 为值设置系统属性时,取决于我设置属性的方式,该属性并不总是可以通过某些方法读回。
我见过Why Map does not work for GString in Groovy? 和Why groovy does not see some values in dictionary?,但我的问题特别适用于地图值,所以不确定这些是否适用?
片段:
def tdollar='dollar'
System.setProperty('key1', 'value1')
System.setProperty('key2', "value2$tdollar")
// Replace the below with any property setting method other than
// the above with the same results
System.properties['key4']='value4'
System.properties['key5']="value5$tdollar"
println System.hasProperty('key1')
println System.hasProperty('key2')
println System.hasProperty('key4')
println System.hasProperty('key5')
println
println System.getProperty('key1')
println System.getProperty('key2')
println System.getProperty('key4')
println System.getProperty('key5')
println
println System.properties.keySet()
println
println System.properties['key1']
println System.properties['key2']
println System.properties['key4']
println System.properties['key5']
输出:
null
null
null
null
value1
value2dollar
value4
null
[java.runtime.name, sun.boot.library.path, java.vm.version, gopherProxySet, java.vm.vendor, java.vendor.url, path.separator, java.vm.name, file.encoding.pkg, user.country, sun.java.launcher, sun.os.patch.level, program.name, key5, key4, java.vm.specification.name, user.dir, key2, java.runtime.version, key1, java.awt.graphicsenv, java.endorsed.dirs, os.arch, java.io.tmpdir, line.separator, java.vm.specification.vendor, os.name, tools.jar, sun.jnu.encoding, script.name, java.library.path, java.specification.name, java.class.version, sun.management.compiler, os.version, user.home, user.timezone, java.awt.printerjob, file.encoding, java.specification.version, java.class.path, user.name, java.vm.specification.version, sun.java.command, java.home, sun.arch.data.model, user.language, java.specification.vendor, awt.toolkit, java.vm.info, java.version, java.ext.dirs, sun.boot.class.path, java.vendor, file.separator, java.vendor.url.bug, sun.io.unicode.encoding, sun.cpu.endian, groovy.starter.conf, groovy.home, sun.cpu.isalist]
value1
value2dollar
value4
value5dollar
为什么,如果我不使用System.setProperty(key, value) 语法,属性不能通过System.getProperty(key) 读取,但仍然可以通过任何其他方法读取?
鉴于这种行为,是否有关于 Groovy 中系统属性的最佳实践记录在案。
写到这里,我想知道这是否只是一个一般的地图问题。将测试。
【问题讨论】:
-
查看这个答案以深入了解 java
Properties对象的奇怪行为:stackoverflow.com/questions/39038580/…
标签: groovy properties maps