How to externalise configuration variable for non Spring application in java

470 Views Asked by At

I am using jdk 8 and am trying to externalise database connection variables in my java application. There is one way to do this like System.getEnv("key") but I dont need to use this.

Can anyone help to achieve this without Spring.

my configuration,

dbHost: localhost
dbPort: 8091
bucketName: demo_bucket

Kindly provide your inputs.

1

There are 1 best solutions below

5
On

You can use Properties object in java for inline configuration.

java.util.Properties dbConfig = new java.util.Properties();
dbConfig.put("url", <your_db_url>);
dbConfig.put("user", <db_user>);
dbConfig.put("password", <db_user_password>);

pass dbConfig object to your factory that builds your connection object.

Another way, You can use yml file to store your configuration and then read that file and create a configuration object, configuration object pass across your application architecture. It gives the flexibility to manage your configuration according to your criteria.