java 多个属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5093432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:23:42  来源:igfitidea点击:

Multiple properties files

javaproperties

提问by Swift-Tuttle

In a java application, I am using .properties file to access application related config properties.
For eg.
AppConfig.propertiesthe contents of which are say,

在 java 应用程序中,我使用 .properties 文件来访问与应用程序相关的配置属性。
例如。
AppConfig.properties其中的内容是说,

settings.user1.name=userone
settings.user2.name=usertwo
settings.user1.password=Passwrd1!
settings.user2.password=Passwrd2!  

I am accesing these properties through a java file - AppConfiguration.javalike

我正在通过一个 java 文件访问这些属性 -AppConfiguration.java比如

    private final Properties properties = new Properties();
    public AppConfiguration(){
        properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig.properties"));
}

Now, instead of keeping all the key-value properties in one file, I would like to divide them in few files(AppConfig1.properties, AppConfig2.properties, AppConfig3.properties etc.).
I would like to know if it is possible to load these multiple files simultaneously.

现在,我不想将所有键值属性保存在一个文件中,而是将它们分成几个文件(AppConfig1.properties、AppConfig2.properties、AppConfig3.properties 等)。
我想知道是否可以同时加载这些多个文件。

My question is not similar to - Multiple .properties files in a Java project

我的问题与 - Java 项目中的多个 .properties 文件不同

Thank You.

谢谢。

回答by adarshr

Yes. Simply have multiple load statements.

是的。只需有多个加载语句。

properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig1.properties"));
properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig2.properties"));
properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig2.properties"));

All the key-value pairs will be available to use using the properties object.

所有的键值对都可以使用属性对象使用。

回答by duffymo

If I understand your question, you have two objectives in mind:

如果我理解你的问题,你有两个目标:

  1. Partition one very large .properties file into several smaller ones where the (key, value) pairs are related.
  2. Ensure that all .properties files are available at the same time, even if you read them simultaneously using threads.
  1. 将一个非常大的 .properties 文件分成几个较小的文件,其中 (key, value) 对是相关的。
  2. 确保所有 .properties 文件同时可用,即使您使用线程同时读取它们。

If that's the case, I'd proceed with partitioning the .properties into several files and write a new class that handles the reading of individual .properties files and the merging of all the results into a single Properties instance.

如果是这种情况,我会继续将 .properties 分成几个文件并编写一个新类来处理单个 .properties 文件的读取并将所有结果合并到一个 Properties 实例中。

回答by Riduidel

As Propertiesobjects are in fact map, you can use all of their methods, including putAll(...). In your case, it would be useful to load each Property file using a separate Properties object, then merge them in your application properties.

由于Properties对象实际上是映射,因此您可以使用它们的所有方法,包括putAll(...). 在您的情况下,使用单独的 Properties 对象加载每个 Property 文件,然后将它们合并到您的应用程序属性中会很有用。

回答by user1573424

I have 2 solutions for you:

我有两个解决方案给你:

  • You can have different properties object for different properties files
  • You can merge them using putAll().

    Properties properties1 = new Properties(); properties1.load(Thread.currentThread().getContextClassLoader() .getResourceAsStream("AppConfig1.properties")); Properties properties2 = new Properties(); properties.load(Thread.currentThread().getContextClassLoader() .getResourceAsStream("AppConfig2.properties")); Properties merged = new Properties(); merged.putAll(properties1); merged.putAll(properties2);

  • 您可以为不同的属性文件使用不同的属性对象
  • 您可以使用putAll().

    Properties properties1 = new Properties(); properties1.load(Thread.currentThread().getContextClassLoader() .getResourceAsStream("AppConfig1.properties")); Properties properties2 = new Properties(); properties.load(Thread.currentThread().getContextClassLoader() .getResourceAsStream("AppConfig2.properties")); Properties merged = new Properties(); merged.putAll(properties1); merged.putAll(properties2);