标签: apache-commons-config

使用apache commons配置XMLConfiguration格式化XML输出

我正在使用apache commons配置XMLConfiguration来构建和保存XML文件.保存时没有格式化.我有类似的东西:

<root>
<node>
<child>
</child>
</node>
</root>
Run Code Online (Sandbox Code Playgroud)

我知道有很多方法可以使用其他库来获取输出并对其进行格式化,但是肯定必须有一种方法来设置像公共配置中的缩进一样简单的东西吗?

java xml-serialization apache-commons-config

6
推荐指数
1
解决办法
4385
查看次数

任何Apache Commons-Configuration替换/竞争对手?

我最近尝试使用Apache Commons-Configuration来管理一些本地XML配置文件.它大大降低了易用性(无法加载空配置文件,CombinedConfiguration需要利用大多数操作的底层配置等),以及其API的一致性(保存操作帖子没有事件,事件不是通用的).

除了写入注册表的JDK首选项(我不想要),还有其他替代方法来管理基于文件的首选项吗?

使用其他文件格式不是一种选择.

java configuration xml-configuration apache-commons-config

6
推荐指数
1
解决办法
2375
查看次数

我可以在Apache Commons Config中同时使用数组和非数组吗?

使用以下属性文件:

foo=hello, world!
bar=first,second
Run Code Online (Sandbox Code Playgroud)

我想将第一项检索为字符串,第二项检索为数组.我本以为getStringvs getStringArray会处理这个,但它不会 - getString("foo")只是在逗号之前获取所有内容,即"你好".

如果我禁用分隔符解析使用setDelimiterParsingDisabled,foo很好,但这也改变了getStringArray("bar")返回单元素数组的行为!

我无法找到如何明确告诉它我希望它如何解释单个配置项,无论是字符串还是数组.我不想将配置项放入具有不同分隔符规则的单独配置文件中,我更喜欢使用逗号作为getStringArray案例的分隔符.

详细说明,这个片段打印hello - 2hello, world! - 1- 我希望它打印hello, world! - 2!

AbstractFileConfiguration config = new PropertiesConfiguration();
config.setFileName("C:\\temp\\temp.properties");
//config.setDelimiterParsingDisabled(true);
config.load();
System.out.println(config.getString("foo") + " - " + config.getStringArray("bar").length);
Run Code Online (Sandbox Code Playgroud)

java configuration apache-commons-config

6
推荐指数
1
解决办法
3276
查看次数

NoClassDefFoundError:org/apache/commons/configuration/ConfigurationException

我有一个应用程序,我需要解析配置文件,当在服务器上运行此程序时,它提供以下跟踪:

java.lang.NoClassDefFoundError: org/apache/commons/configuration/ConfigurationException
    at com.messagedna.server.startup.StartupServlet.init(StartupServlet.java:19)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1274)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5320)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1553)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1486)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:96)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1327)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1419)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:847)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601) …
Run Code Online (Sandbox Code Playgroud)

java apache-commons maven apache-commons-config

6
推荐指数
1
解决办法
3万
查看次数

Apache Commons 配置验证属性文件

我正在使用Apache Commons Configuration库与PropertiesConfiguration. 我的应用程序在启动后立即加载配置文件,如下所示:

public PropertiesConfiguration loadConfigFile(File configFile) throws ConfigurationNotFoundException {
        try {
            if (configFile != null && configFile.exists()) {
                config.load(configFile);
                config.setListDelimiter(';');
                config.setAutoSave(true);
                config.setReloadingStrategy(new FileChangedReloadingStrategy());
                setConfigLoaded(true);
            }
            else {
                throw new ConfigurationNotFoundException("Configuration file not found.");
            }

        } catch (ConfigurationException e) {
            logger.warn(e.getMessage());
            setDefaultConfigValues(config);
            config.setFile(configFile);
        }
        return config;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何验证configFile,以便我可以确定该文件中没有丢失任何属性,并且稍后在我的代码中NullPointerException尝试访问属性时我不会得到 a ,例如:

PropertiesConfiguration config = loadConfig(configFile);
String rootDir = config.getString("paths.download"); // I want to be sure that this property exists right at the …
Run Code Online (Sandbox Code Playgroud)

java validation properties-file apache-commons-config

6
推荐指数
1
解决办法
1720
查看次数

使用apache commons配置xpath查询带有属性的空XML元素

我正在使用apache commons配置XMLConfiguration对象和XPATH表达式引擎来查询XML文件.我是xpath和apache commons的新手,我的语法有问题.

xml文件如下所示:

<attrs>
    <attr name="my_name" val="the_val"/>
    <attr name="my_name2" val="the_val2"/>
</attrs>
Run Code Online (Sandbox Code Playgroud)

我想要做的基本上是公共循环遍历所有属性并读取每行的名称和val.我可以解决所有问题的唯一方法是使用name的值再次查询xml.这种感觉对我来说不对,有没有更好的方法呢?

List<String> names = config.getList("attrs/attr/@name");
for( String name : names )
{
    String val = config.getString("attrs/attr[@name='" +name +"']/@val" );
    System.out.println("name:" +name +"   VAL:" +val);
}
Run Code Online (Sandbox Code Playgroud)

此外转换顶部到String,我不确定正确的方法来处理它.

java xml xpath apache-commons apache-commons-config

5
推荐指数
1
解决办法
2773
查看次数

使用Apache Commons Configuration在属性文件中使用值列表进行变量插值,即$ {variable}

我正在使用Apache Commons Configuration来读取属性文件,我完全能够进行变量插值,并且还可以将多值属性作为列表进行检索.但是,我无法正确加载具有多个值的属性,其中一个属性是另一个多值属性的引用(变量插值).

这是我的属性文件的示例(我也尝试使用逗号分隔语法):

doc.mime=application/msword
doc.mime=application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc.mime=${office.mime}

office.mime=application/x-tika-msoffice
office.mime=application/x-tika-ooxml
Run Code Online (Sandbox Code Playgroud)

以及我如何阅读它:

Configuration config = new PropertiesConfiguration("myFile");
final String[] mimesArray = config.getStringArray("doc.mime");
for(String mime : mimesArray) System.out.println(mime);
final List<Object> mimesList = config.getList("doc.mime");
System.out.println(mimesList);
Run Code Online (Sandbox Code Playgroud)

这是我用任何一种方法得到的内容(getStringArraygetList):

[application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/x-tika-msoffice]

这跟我想象中的不同:两者的全部内容doc.mimeoffice.mime

有谁知道是否有可能在我的其他列表中插入整个值列表?如果是这样,它是如何完成的?

java properties apache-commons-config

5
推荐指数
1
解决办法
6136
查看次数

Netflix archaius 无法读取更新的属性文件值

我是 Netflix archaius 的新手。我有一个读取 Java 属性文件并打印属性值的代码片段。

当该程序运行时,它会打印 testproperty.properties 文件中名为“Fields”的属性的值。现在,当该程序运行时,我正在更新“Fields”属性的值,因此 archaius 应该动态获取更改值。但它仍在打印旧值。

在这个 Java 程序中使用 archaius 的正确方法是什么?或者在不重新启动程序的情况下更新程序中的属性?如果有人可以指出此代码片段中的更正,那将会很有帮助。

我想用Netflix archaius运行一个演示,所以我在我的项目中通过maven导入了archaius。

现在我正在更新我的属性文件。但它仍然打印旧的财产价值。(PS:我在驱动程序中保留了连续的 while 循环,以查看 archaius 是否在运行时选择更新属性值。我想这就是 archaius 应该做的。获取更新的属性而不重新启动应用程序。如果我错了,请纠正我。)

下面是我的代码片段:

import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;

public class PropertyChangetest {

    public static void main(String args[]) {

        DynamicPropertyFactory sampleProp = DynamicPropertyFactory.getInstance();
        System.setProperty("archaius.configurationSource.defaultFileName", "TestProperty.properties");
        System.setProperty("archaius.fixedDelayPollingScheduler.delayMills", "500");

        while(true) {
            DynamicStringProperty sampleProp1 = sampleProp.getStringProperty("fields","");
            System.out.println(sampleProp1.get());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的“TestProperty.properties”文件只有一个名为“字段”的属性。运行程序后,我正在更新我的属性文件,但它仍然打印旧值。

java netflix configuration-management apache-commons-config netflix-archaius

5
推荐指数
1
解决办法
2529
查看次数

Gradle 使用具有依赖项的插件抛出 ClassNotFoundException

ClassNotFoundException在同一根文件夹中的项目中使用带有编译依赖项的 Gradle 插件时,我遇到了s。

目录结构相当简单,有一个插件和将使用它的项目:

    • buildSrc
    • demo-project

build.gradlebuildSrc如下:

apply plugin: 'groovy'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}

dependencies {
    compile gradleApi()
    compile localGroovy()

    compile 'org.apache.commons:commons-configuration2:2.0'
    compile 'commons-beanutils:commons-beanutils:1.9.2'
}
Run Code Online (Sandbox Code Playgroud)

插件本身构建正确。

将插件应用到 my 时demo-project,该afterEvaluate部分失败,因为它找不到commons-configuration2依赖项(因此插件执行,而不是在有罪的行上失败):

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':demo-project'.
> java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration
Run Code Online (Sandbox Code Playgroud)

只是为了完成我的问题,这是build.gradle我的demo-project

apply plugin: 'java'
apply plugin: 'com.sample.myplugin'

thisIsMyPluginConfiguration {
    something = 'here'
} …
Run Code Online (Sandbox Code Playgroud)

apache-commons gradle apache-commons-config gradle-plugin

5
推荐指数
1
解决办法
1636
查看次数

组合配置文件

我正在开发一个 jee 应用程序,它必须查看两个文件才能加载配置参数。这两个文件都是类似属性的文件。

第一个包含默认配置属性,另一个覆盖它们。所以第一个是只读的,另一个可以修改。我需要做出反应并更新对第二个配置文件所做的更改。

我查看了几个资源:

我无法弄清楚如何制定配置策略以及如何制定配置策略commons-configuration2.

到目前为止,我已经能够从一个配置文件中读取:

FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
    new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
        .configure(new Parameters().properties()
            .setFileName(ConfigurationResources.PROPERTIES_FILEPATH)
            .setThrowExceptionOnMissing(true)
            .setListDelimiterHandler(new DefaultListDelimiterHandler(';'))
            .setIncludesAllowed(false));
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

apache-commons apache-commons-config

5
推荐指数
1
解决办法
2516
查看次数