Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple properties files

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

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.java like

    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.

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

Thank You.

like image 771
Swift-Tuttle Avatar asked Mar 03 '26 11:03

Swift-Tuttle


1 Answers

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.

like image 61
adarshr Avatar answered Mar 05 '26 02:03

adarshr