Apache Commons配置 - 无法加载/使用配置文件

时间:2012-04-17 18:50:15

标签: java configuration apache-commons

我一直在尝试配置Java应用程序(特别是我的集成测试),但绝对没有成功。我最初有一个XMLConfiguration,我可以加载和使用没有问题。我的目录设置如下:

  • SRC
    • 测试
      • 的java
        • mypackage的
      • 资源

之前,我在资源目录中有一个文件myconfig.xml。我是这样装的:

private static XMLConfiguration configuration = null;
public static void configure() {
    try {
        configuration = new XMLConfiguration("myconfig.xml");
    }
    catch (ConfigurationException e) {
        System.err.println(e);
    }
}

现在我正在努力使用户可以通过在/ etc或/ home或其他任何地方编写配置文件来自行配置应用程序,因此我在同一位置创建了一个名为config-test.xml的新文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
    <!-- Attempt to load configuration provided on the system -->
    <xml fileName="/etc/myconf/myconfig.xml" config-optional="true" />

    <!-- Load default configuration from the classpath -->
    <xml fileName="myconfig.xml" />
</configuration>

文件/etc/myconf/myconfig.xml不存在,但这是使其成为可选的重点。我尝试了一些不同的方法来加载我的配置,包括文档的工作方式:http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html。我目前的情况如下:

private static CombinedConfiguration configuration = null;
public static void configure() {
    try {
        DefaultConfigurationBuilder builder = 
            new DefaultConfigurationBuilder("config-test.xml");
        configuration = builder.getConfiguration(true);
        System.err.println("Yay");
    }
    catch (ConfigurationException e) {
        System.err.println("Herp");
    }
    catch (Exception e) {
        System.err.println("Derp");
    }
}

当我运行我的集成测试时,我看不到Yay,我看不到Herp,我看不到Derp。如果我在尝试加载内容之前放置println,则会打印,因此正在调用configure。我从公共配置获得的输出相当于:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
13:36:57.752 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.753 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.756 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.766 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
13:36:57.795 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.795 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.796 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.800 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16, Time elapsed: 2.589 sec <<< FAILURE!

Results :

Failed tests:   initialize(mypackage.ImportTestIT): org/apache/commons/beanutils/PropertyUtils
  initialize(mypackage.TransferTestIT): org/apache/commons/beanutils/PropertyUtils

Tests run: 56, Failures: 2, Errors: 0, Skipped: 16

这些初始化方法只是调用configure,然后尝试从加载的配置中获取数据。我一直在用这个工作几个小时,真的可以用另一双眼睛。有任何想法吗?有什么我可以改变的,我没有提到会影响这个吗?感谢。

编辑#1(下午1:58):确定beanutils在类路径上后,我现在得到一个以

开头的堆栈跟踪的残酷爆炸
Running TestSuite
14:00:16.088 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
14:00:16.089 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
14:00:16.093 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
14:00:16.103 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
14:00:16.178 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml, name is /etc/myconf/myproject/myconfig.xml
14:00:16.179 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file /etc/myconf/myproject/myconfig.xml at file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml: /etc/myconf/myproject/myconfig.xml (No such file or directory)
14:00:16.181 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Load failed for optional configuration xml: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
14:00:16.185 [main] WARN  o.a.c.c.DefaultConfigurationBuilder - Internal error
org.apache.commons.configuration.ConfigurationException: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:259) ~[commons-configuration-1.8.jar:1.8]
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:238) ~[commons-configuration-1.8.jar:1.8]

依此类推......但我现在确实看到了'耶'。 现在我没有得到的是它似乎没有寻找我的非可选配置源,或者假设它位于我从未告诉它的位置。

编辑#2: 我仔细查看了我的输出,并埋没在公地的投诉中,我发现它似乎最终加载了正确的文件。我想我可能有工作吗?也许当我加载它时,我的属性路径会改变...我有一些东西要尝试。无论如何,我问过的问题已得到修复。谢谢你的帮助。

2 个答案:

答案 0 :(得分:3)

您似乎在类路径上缺少commons-beanutils。确保罐子在那里。

答案 1 :(得分:1)

Pfft。 multi-page document讨论了如何加载配置文件。但他们是否忘记在绝对Windows路径中实现文件URL的检测,例如c:\ Users \ USER \ FOO \ bar.xml? Commons 1.9将展示这条严谨的信息,

DEBUG DefaultFileSystem - Could not locate file C:\Users\USER\FOO\bar.xml at null: unknown protocol: c

https://github.com/apache/commons-configuration/blob/CONFIGURATION_1_9/src/main/java/org/apache/commons/configuration/DefaultFileSystem.java#L281