When you develop any application it should be properly configured.
Usually the configuration data is stored as property files (for example application.properties
, db.properties
etc).
There are many places where our application configs can be stored:
- the resource folder in your application - property files go with your application in the same jar file.
- the user home folder on your computer - usually this is how people configure the application for development and testing locally.
- any other folders on your computer or on the server where the application is running
- any places in the network where the configs can be pulled from
Also the application can be configured with the environmental variables that are usually set at the deployment time.
AppProps Maven dependency
On the date of the post.
<dependency>
<groupId>io.lenar</groupId>
<artifactId>app-props</artifactId>
<version>1.1.0</version>
</dependency>
UPD: The AppProps functionality is constantly changing (see https://github.com/LenarBad/app-props)
Create AppProps in application
All you need to do to make all property files accessible for application is to specify what files and where they should be located.
AppProps appProps = new AppProps();
Once you instantiate the AppProps
you can read any environmental variable same way as you do that with System.getProperty(..)
appProps.value("host");
appProps.value("protocol", "https");
Now you need to specify all other sources of property files
Property files in Resource folder
appProps.resourcePropFile("application.properties")
.resourcePropFile("db.properties");
Now you can access any property from these files with appProps.value(..)
Property files in User Home folder
appProps.homeDirPropFile("test.properties");
Property files in any other folders
appProps.fileSystemPropFile("custom.properties");
Network resources
appProp.networkPropResource("remote.properties");
Reload configs
If you need refresh all your properties then just call the reload()
method.
appProp.reload();
Disable auto loading
If you don’t want all the configs to be automatically loaded/reloaded then you need to instantiate AppProp
just like this
AppProps appProps = new AppProps(false);
In this case all configuration properties from all files and environmental variables will be loaded only when you explicitly call reload()
.
AppProps appProps = new AppProps(false);
appProps.resourcePropFile("application.properties")
.resourcePropFile("db.properties")
.homeDirPropFile("test.properties")
.fileSystemPropFile("custom.properties")
.networkPropResource("remote.properties");
appProps.reload();
appProps.value("host");
appProps.value("protocol", "https");
Overriding properties
Remember the order you adding property files matters.
If a property exists in more than one files then the last value will override all previous values.
This mechanism allows us to use for example .homeDirPropFile("test.properties")
to override configuration properties when you run the application or tests locally on your computer - just place in the end.
The current version of AppProps -
Give AppProps a star on GitHub if you like it.
You may also find these posts interesting: