简体   繁体   English

Apache Commons 配置:从类路径/JAR 读取?

[英]Apache Commons Configuration: read from classpath/JAR?

Does the Apache Commons Configuration library support reading properties/configuration files from the classpath or JAR? Apache Commons Configuration 库是否支持从类路径或 JAR 读取属性/配置文件? I didn't find an API where it can take an InputStream , that returned by getResourceAsStream .我没有找到 API 可以接受由getResourceAsStream返回的InputStream的地方。

Based on @dkb comment, and since there is not yet an answer to the question, you can do the following:基于@dkb 评论,由于问题还没有答案,您可以执行以下操作:

    Configuration configuration = new FileBasedConfigurationBuilder<FileBasedConfiguration>(
       PropertiesConfiguration.class)
          .configure(new Parameters()
                           .properties()
                           .setFileName("app.properties"))
          .getConfiguration());

You can also specify the location strategy that you want to use:您还可以指定要使用的位置策略:

Configuration configuration = new FileBasedConfigurationBuilder<FileBasedConfiguration(
   PropertiesConfiguration.class)
      .configure(
          new Parameters()
                .properties()
                .setLocationStrategy(new ClasspathLocationStrategy())
                .setFileName("app.properties"))
       .getConfiguration());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM