简单的 Apache-commons 配置示例抛出 ConfigurationRuntimeException [英] Simple Apache-commons configuration example throws ConfigurationRuntimeException

查看:36
本文介绍了简单的 Apache-commons 配置示例抛出 ConfigurationRuntimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个非常简单的例子 在 Apache-commons 配置库用户指南中给出,关于声明和创建 bean.我几乎逐字复制了示例中的代码,但我得到了 ConfigurationRuntimeException(这是在克服了不同的异常之后,请参阅 this问题).

I'm trying to test a very simple example given in the Apache-commons configuration library user's guide regarding declaring and creating beans. I copied the code in the example almost word by word, and yet I'm getting a ConfigurationRuntimeException (this after overcoming a different exception, see this question).

这是我正在使用的 xml 文件 - windowcongif.xml:

Here is the xml file I'm using - 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

我如何使这个简单的示例工作?

我使用的是 1.9 版的 commons-configuration 包和 1.8.3 版的 commons-beanutils 包,在将依赖项放入 pom.xml 文件后由 IntelliJ IDEA 自动导入,以及在 Windows 8 64 位上运行的 Java 版本 1.7.0_17.

I'm using version 1.9 of the commons-configuration package and version 1.8.3 of the commons-beanutils package, auto-imported by IntelliJ IDEA after putting the dependencies in the pom.xml file, and version 1.7.0_17 of java running on Windows 8 64bit.

推荐答案

如果您使用的是 JavaBeans,则需要为要设置的每个字段添加一个 setter.

If you are using JavaBeans you will need to add a setter for each field you want to set.

我建议在 IntelliJ 中为这些字段使用 add setter 和 getter.

I suggest using the add setter and getter in IntelliJ for these fields.

示例说明

// getters and setters ommitted, also the WindowManager methods

这篇关于简单的 Apache-commons 配置示例抛出 ConfigurationRuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆