[英]Is there an equivalent class for groovy's ConfigSlurper in java?
Below is a small example that I generally use in groovy, but I want something similar in java. 下面是一个我通常在groovy中使用的小示例,但是我想要在Java中类似的东西。
Configuration file 配置文件
datastore{
oracle{
host="localhost"
port=1521
dbname="orcl"
}
db2{
host="localhost"
port=12807
dbname="sppd"
}
}
Groovy Groovy的
public class Configuration {
public static final def cfg = new ConfigSlurper().parse(new File("configuration").toURL())
static main(args) {
println cfg.datastore.oracle.host
println cfg.datastore.db2.host
}
}
I'm guessing that you're wanting to access a property by its full path, eg datastore.oracle.host
from Java like you can in Groovy. 我猜想您想通过属性的完整路径来访问属性,例如,像在Groovy中一样,从Java中访问
datastore.oracle.host
。 If so, do this: 如果是这样,请执行以下操作:
ConfigObject config = new ConfigSlurper().parse(new File("configuration").toURL());
Map flattenedConfig = config.flatten();
String oracleHost = (String) flattenedConfig.get("datastore.oracle.host");
Better than Java properties because the type is maintained. 比Java属性更好,因为可以保留类型。 From a Groovy User list post .
在Groovy用户列表中 。
That is too much dynamic groovyness, but can be expressed in XML, properties, etc. You can try Common Configuration
, which would express the same data: 这太动态了,但是可以用XML,属性等表示。您可以尝试使用
Common Configuration
,它可以表示相同的数据:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
</gui-definition>
And read with: 并阅读:
XMLConfiguration config = new XMLConfiguration("tables.xml");
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
String linkNormal = config.getString("colors.link[@normal]");
You can also give a try to PropertyConfiguration
: 您也可以尝试
PropertyConfiguration
:
# Properties definining the GUI
colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
Loaded with: 满载:
Configuration config = new PropertiesConfiguration("usergui.properties");
String backColor = config.getString("colors.background");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.