user2433924
user2433924

Reputation: 49

Java Properties - Global and local properties

So I'm beginning to use the Java Properties class in order to set key-value pairs for my project. The way I'm designing my project is so that there are default properties which will be created using a config file as well as another config file for either overwriting or adding additional properties. The default config file will be in my Eclipse MainFramework project while the other config file will be in the local project where tests are stored.

MainFramework
Validation
TestProject1
TestProject2

In this example, MainFramework has the default config file and each TestProject may or may not have it's own local config file. Is there a way to have my desired functionality through Java's Properties class.

Upvotes: 1

Views: 1982

Answers (2)

Amitay Dobo
Amitay Dobo

Reputation: 1388

How about using a 3rd party configuration library to achieve this?

Typesafe's config supports the usage of properties files, and can handle merging a global configuration with a subconfiguration, among many other features.

Apache commons configuration also supports property files as configuration sources and mechanisms for combining different sources.

I personally found Typesafe a bit easier to understand and use, but have a look at some examples to see what fits your style. They are both available through maven.

Upvotes: 0

Rick Mangi
Rick Mangi

Reputation: 3769

The java properties object is a Hashtable. If you read the properties for your main configuration file and then read a second properties file into the same object it will override the existing properties if they exist in both places, or add new ones if they don't already exist. Properties that are only found in the original file will remain as well.

Upvotes: 1

Related Questions