An easy to use and lightweight Java library for retrieving configuration properties backed by an enum
. Supports multi-lined value and variable substitution (e.g. $PATH
or ${PATH}
as in bash syntax) in the values. Use of enum for defining the property keys will make referencing source less error prone and easy to refactor, as far as this library is concerned
- All property keys are defined in a single place, i.e. a user defined
enum
- Property values can contain variables (starting with
$
sign, e.g.$PATH
) wherePATH
is a property key in same file - Property value can be obtained as specified data type, so no need to convert string value to required data type
- Property value can be obtained as list of specified data types
- Property value can be a multi-line text
- Can make property keys mandatory or optional
- Can specify default value for the property key if value is not available
- Is thread safe
While using property files are often preferred for defining application configurations due to their simplicity and readability, they also have certain disadvantages. One of the major problem in using property files is that property keys are in the form of String
, so it is difficult to manage. If we were to change some key, we need to manually search its occurrences and update all of them. This makes our code fragile and cumbersome. So, this library is designed to provide enum
backed configuration property. Due to use of enum
, all the property keys are defined in a single place, and since enum constants are referred to instead of plain String
s, refactoring the code becomes easy and less error prone. The APIs also provide methods for retrieval of the values in form of data type that we want. Have a look at Javadocs for details
- JRE 1.6 or above
- First of all, include jar for this library in your project. For example, if you are using maven, add below given dependency to your pom.xml file.
<dependency>
<groupId>com.github.easy-develop</groupId>
<artifactId>properties</artifactId>
<version>1.0.1</version>
</dependency>
If you are using any other tool for build, have a look at Maven Repository page for corresponding dependency declaration
- Create an
enum
class which will define the property keys, like below example:
public enum MyPropertyKey {
HOME_DIR,
CONF_DIR,
LOG_DIR,
CURRENT_USER_IDS
}
Note: The enum
can be changed to indicate things like whether property key is mandatory or optional, or default value if value is not available. But for our example, we are considering simplest of the cases
- Invoke the APIs to obtain a Properties object which will provide available property values in required format, like below example:
import com.easy.properties.Properties;
import com.easy.properties.PropertiesLoader;
import java.util.List;
public class ReadPropsDemo {
public static void main(String[] args) {
PropertiesLoader propsLoader = new PropertiesLoader("_path_to_property_file_", MyPropertyKey.class);
Properties props = propsLoader.load();
String homeDir = props.get(MyPropertyKey.HOME_DIR);
List<Integer> currentUserIds = props.getList(MyPropertyKey.CURRENT_USER_IDS, int.class);
System.out.println("Home directory is: " + homeDir);
System.out.println("Current users: " + currentUserIds);
}
}
And that's it. Obtained instance of com.easy.properties.Properties
can be used to retrieve value corresponding to certain enum constant in required format (data type) as shown above. The content of configuration property file could be something like below:
HOME_DIR = /home/demo
CONF_DIR = ${HOME_DIR}/conf
LOG_DIR = ${HOME_DIR}/logs
CURRENT_USER_IDS = 1921, 8887, 7746
Have a look at examples if you want to see the usage in different cases
Please open an issue if you need any assistance or have any suggestion regarding this library
This project is licensed under MIT License