getProperty(“。propertyName”)上的Apache Commons CompositeConfiguration XPath异常

时间:2014-09-15 05:38:13

标签: java xpath apache-commons xml-configuration apache-commons-config

我有一个CompositeConfiguration对象,它有一些属性配置和一些XMLConfiguration。对于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遍历每个配置并使用在这种情况下为“.propertyName”的键调用该配置上的getProperty。 当XPathExpression Engine用于从XMLConfiguration获取值时,它会破坏无效的xpath。

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

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

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

谢谢, 特加斯

1 个答案:

答案 0 :(得分: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:How to load multiple configuration files using apache common configuration(java)