Multiple Property Files

In a java application, I use the .properties file to access the configuration properties associated with the application.
For example, the
AppConfig.propertiescontents of which we say

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

I use these properties through a java file - AppConfiguration.javalike

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

Now, instead of saving all the key properties in one file, I would like to split them into several files (AppConfig1.properties, AppConfig2.properties, AppConfig3.properties, etc.). I would like to know if these multiple files can be uploaded at the same time.

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

Thank.

+3
source share
4 answers

Yes. Just follow some download instructions.

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

- .

+8

, :

  • .properties , (, ).
  • , .properties , .

.properties , .properties Properties.

0

Properties , , putAll(...). Properties, .

0

2 :

  • You may have a different property object for different property files.
  • You can combine them with 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);

0
source

Source: https://habr.com/ru/post/1794180/


All Articles