非常简单的Apache-commons配置检查会抛出ConfigurationRuntimeException
我正在尝试测试Apache-commons配置库用户指南中有关声明和创建bean的一个非常简单的示例 。 我几乎一字一句地复制了示例中的代码,但是我得到了一个ConfigurationRuntimeException(在克服了另一个exception之后,请看这个问题 )。
这是我正在使用的xml文件 – windowcongif.xml
:
这是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
我如何使这个简单的例子有效?
我正在使用commons-configuration软件包的1.9版本和commons-beanutils软件包的1.8.3版本,在将依赖项放在pom.xml
文件中后由IntelliJ IDEA自动导入,并运行java版本1.7.0_17 Windows 8 64位。
如果您使用的是JavaBeans,则需要为要设置的每个字段添加一个setter。
我建议在IntelliJ中使用add setter and getter
来获取这些字段。
这个例子说明了
// getters and setters ommitted, also the WindowManager methods