This is a simple library to parse Config files of different formats into POJO's. It can directly be dropped into your Maven / Gradle projects and can be used in the project without any hassle.
To use this library, define your project specific config's POJO in whichever package as you may wish. Then define your POJO's fields using lowerCaseNamingConvention, and generate getter-setters for each field accordingly. Finally, define default values for your config class in it's constructor.
ServiceSetting.java
import com.github.wasiqb.coteafs.config.util.BasePojo;
public class ServiceSetting extends BasePojo {
private int port;
private String type;
private String url;
// Define default values for the config.
public ServiceSetting () {
this.apiPort = 3000;
this.apiType = "Rest";
this.apiUrl = "https://localhost";
}
public int getPort () {
return this.port;
}
public String getType () {
return this.type;
}
public String getUrl () {
return this.url;
}
public void setPort (final int port) {
this.port = port;
}
public void setType (final String type) {
this.type = type;
}
public void setUrl (final String url) {
this.url = url;
}
}
Once this is done, now you just need to define your POJO's corresponding config file for your project, as shown below:
Pro Tip! If you don't want to create the config file manually, then no need to worry, This library will create the config file for you with default values set in the constructor.
test-config.yaml
url: http://localhost
port: 8080
type: ${ENV_TYPE} # Environment variable placeholders are allowed.
test-config.json
{
"api_url": "http://localhost",
"api_port": 8080,
"api_type": "SOAP"
}
test-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ServiceSetting>
<api_url>http://localhost</api_url>
<api_port>8080</api_port>
<api_type>SOAP</api_type>
</ServiceSetting>
test-config.properties
api_url = http://localhost
api_port = 8080
api_type = SOAP
Additional Tip!! This library also supports Environment and System property placeholders in the config file for string fields but only for YAML and JSON format configs.
Make sure that config keys are lower_case_with_words_separated_with_underscore.
The config file path can be provided in Environment variable with key coteafs.config
, if it is not defined, then it will search System property with same key, if that is also not available then by default, it will search for file named test-config.yaml
under src/test/resources
directory.
You can define your own key by using withKey
method of ConfigLoader
class. You can also define default file name which can be found under resources folder using withDefault
method.
import static com.github.wasiqb.coteafs.config.loader.ConfigLoader.settings;
. . .
ServiceSetting setting = settings ().withKey ("coteafs.xyz.setting")
.withDefault ("test-config-xyz.json")
.load (ServiceSetting.class);
. . .
You can use the following dependency into your pom.xml
to use this library.
<dependency>
<groupId>com.github.wasiqb.coteafs</groupId>
<artifactId>configs</artifactId>
<version>2.2.0</version>
</dependency>
Jar files can be directly downloaded from Release tab.
- Directly chat with me on my site and I'll revert to you as soon as possible.
- Discuss your queries by writing to me @ wasbhamla2005@gmail.com
- If you find any issue which is bottleneck for you, search the issue tracker to see if it is already raised.
- If not raised, then you can create a new issue with required details as mentioned in the issue template.
- Spread the word with your network.
- Star the project to make the project popular.
- Stay updated with the project progress by Watching it.
- Contribute to fix open issues, documentations or add new features. To know more, see our contributing page.
- I would be delighted if you can Sponsor this project and provide your support to open source development by clicking on the Sponsor button on the top of this repository.