简体   繁体   中英

How to externalise configuration variable for non Spring application in java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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