What is general Apache configuration
Apache Commons configuration is best known for the library's ability to work with configuration files, that is, parse in a file .properties
.
For example:
color=Green
person=John
This properties file can be in the classpath or hard directory. Using Apache Commons Configurations, you can easily parse the parse and get the value represented by its key.
See this quick tutorial .
EDIT
But why Apache Commons config or configs?
Sometimes you don't want to hardcode a specific value in codes that need to be compiled. For example, you might have an application variable BACKGROUND_COLOR , the value of this variable controls the background color of your application. How can you keep this in your application?
You can do it:
public static final String BACKGROUND_COLOR = "Green";
However, if you want to change the background color to "Red", you will have to change the above code, recompile to:
public static final String BACKGROUND_COLOR = "Red";
What if you don't want to change your codes, recompile to change the background of your application? Yes, you can save this value to a text file with a name system.properties
or to any name and extension.
For example, you can store it in system.properties:
background_color=Green
But how do you read this text file? (which saves in properties format, key=value
). Do you want to go to low level file and IO to read these values? Chances are you won't, you would like a mature and installed library to do it for you.
You can use the Apache Commons configuration for this purpose. This library is for reading configurations such as a properties file.
Using Apache Commons configs, here are the codes to read the specified properties file and extract the key value background_color
.
public static void main(String [] args){
Configuration config = new PropertiesConfiguration("system.properties");
String backColor = config.getString("background_color");
System.out.println(backColor); // this will give you green
}
Hope this helps your understanding. :)
source to share
Apache Commons Configuration is a Java library that makes it easy to manage application configuration properties. It allows you to collect properties from different configuration sources like properties files, XML files, Java System properties, environment variables, etc. It also allows you to override properties along the preference chain of config source settings. Refer to this article .
source to share