我想要测试a very simple example在Apache公共配置库用户指南中有关声明和创建bean的a very simple example。我几乎一字一句地复制了代码,但是我得到了一个ConfigurationRuntimeException(在克服了另一个异常之后,请参阅this question)。非常简单的Apache公共配置检查抛出ConfigurationRuntimeException
这里是我使用的XML文件 - windowcongif.xml
:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<gui>
<windowManager config-class="test.DefaultWindowManager"
closable="false" resizable="true" defaultWidth="400"
defaultHeight="250">
</windowManager>
</gui>
</config>
这里是文件WindowManager.java
代码:
package test;
public interface WindowManager {}
这里是文件DefaultWindowManager.java
代码:
package test;
public class DefaultWindowManager implements WindowManager {
private boolean resizable;
private boolean closable;
private int defaultWidth;
private int defaultHeight;
}
这是文件中的代码Main.java
:
package test;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.beanutils.BeanDeclaration;
import org.apache.commons.configuration.beanutils.BeanHelper;
import org.apache.commons.configuration.beanutils.XMLBeanDeclaration;
import org.apache.commons.beanutils.PropertyUtils;
public class Main {
public static void main(String[] args) throws ConfigurationException {
XMLConfiguration config = new XMLConfiguration("windowconfig.xml");
BeanDeclaration decl = new XMLBeanDeclaration(config, "gui.windowManager");
WindowManager wm = (WindowManager) BeanHelper.createBean(decl);
}
}
下面是运行时的输出:
Exception in thread "main" org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: Property defaultHeight cannot be set on test.DefaultWindowManager
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:341)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:358)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:372)
at test.Main.main(Main.java:24)
Caused by: org.apache.commons.configuration.ConfigurationRuntimeException: Property defaultHeight cannot be set on test.DefaultWindowManager
at org.apache.commons.configuration.beanutils.BeanHelper.initProperty(BeanHelper.java:271)
at org.apache.commons.configuration.beanutils.BeanHelper.initBeanProperties(BeanHelper.java:229)
at org.apache.commons.configuration.beanutils.BeanHelper.initBean(BeanHelper.java:166)
at org.apache.commons.configuration.beanutils.DefaultBeanFactory.initBeanInstance(DefaultBeanFactory.java:108)
at org.apache.commons.configuration.beanutils.DefaultBeanFactory.createBean(DefaultBeanFactory.java:64)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:336)
... 3 more
如何使这个简单的例子工作?
我使用公地的BeanUtils包的公共资源配置包和1.8.3版本的1.9版,通过IntelliJ IDEA的自动进口将依赖于pom.xml
文件后,和1.7.0_17版在Windows 8 64位上运行的Java。