0

我有一个 CompositeConfiguration 对象,它有一些属性配置和一些 XMLConfigurations。对于 XMLConfiguration,DefaultExpressionEngine 设置为 XPathExpressionEngine

.properties 文件之一具有以“。”开头的属性。例如:.propertyName="name"

CompositeConfiguration compositeConfiguration = new CompositeConfiguration();

XMLConfiguration xmlConfig = new XMLConfiguration("xmlFile.xml");
xmlConfig.setDefaultExpressionEngine(new XPathExpressionEngine());

compositeConfiguration.addConfiguration(xmlConfig);

compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file1.properties")));
compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file2.properties")));
compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file3.properties"))); 
compositeConfiguration.getProperty(".propertyName");

我得到以下异常:

org.apache.commons.jxpath.JXPathInvalidSyntaxException: Invalid XPath: '.propertyName'. Syntax error after "."
at org.apache.commons.jxpath.ri.Parser.parseExpression(Parser.java:63)
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.compileExpression(JXPathContextReferenceImpl.java:249)
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.iteratePointers(JXPathContextReferenceImpl.java:616)
at org.apache.commons.jxpath.JXPathContext.selectNodes(JXPathContext.java:686)
at org.apache.commons.configuration.tree.xpath.XPathExpressionEngine.query(XPathExpressionEngine.java:183)
at org.apache.commons.configuration.HierarchicalConfiguration.fetchNodeList(HierarchicalConfiguration.java:958)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.fetchNodeList(AbstractHierarchicalFileConfiguration.java:439)
at org.apache.commons.configuration.HierarchicalConfiguration.getProperty(HierarchicalConfiguration.java:344)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.getProperty(AbstractHierarchicalFileConfiguration.java:392)
at org.apache.commons.configuration.HierarchicalConfiguration.containsKey(HierarchicalConfiguration.java:725)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.containsKey(AbstractHierarchicalFileConfiguration.java:360)
at org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)

这个例外是因为 CompositeConfiguration 遍历每个配置并在该配置上调用 getProperty,在这种情况下,键是“.propertyName”。当 XPathExpression Engine 用于从 XMLConfiguration 获取值时,它会中断说无效的 xpath。

这似乎是一个很容易/常见的情况。有没有其他人遇到过类似的问题?有没有办法解决这个问题?

我可以使用任何其他配置类型吗?我正在使用 CompositeConfiguration,因为我想优先使用先加载的属性。

任何提示都会对我有很大帮助。

谢谢,光辉

4

1 回答 1

0

只是猜测,但你是否已经尝试过改变

compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file1.properties")));  

进入

compositeConfiguration.addConfiguration(new PropertiesConfiguration("file1.properties"));  

对于所有三个属性?

也许它只会抛出下一个错误,但这就是这里描述的符号:http: //commons.apache.org/proper/commons-configuration/userguide/overview.html#Mixing_Configuration_Sources

并且在 SO:如何使用 apache common configuration(java) 加载多个配置文件

于 2014-09-15T18:05:30.240 回答