Groovy在此處的行爲使我感到困惑。我已經運行了一些調試器,試圖確定動態混合器中的哪些位置,這些代碼路徑可能會越過,但是想知道是否有人可以直接在這裏設置我。使用Groovy中的GStrings設置/獲取系統屬性時的不同結果
基本上,在爲一個值設置一個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'''對象的'''奇怪的行爲:HTTP:/ /stackoverflow.com/questions/39038580/using-groovy-for-variable-expansion-in-java-properties/39043586#39043586 – BalRog