2

我正在尝试测试Apache-commons 配置库用户指南中关于声明和创建 bean 的一个非常简单的示例。我几乎逐字复制了示例中的代码,但我得到了一个 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

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

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

4

1 回答 1

4

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

我建议对这些字段使用 IntelliJ 中的 add setter and getter

该示例指出

// getters and setters ommitted, also the WindowManager methods
于 2013-04-28T18:59:28.533 回答