If you have ever worked with Spring Framework in Java, know the comfort and flexibility of the application configuration that comes with Spring.
- Have the configuration stored in properties or YML files
- Overriding default configuration based on profiles
- Overriding that with environment variables
Adding to these it also has other 'springy' features like value injection and couple of other property sources. But if you need a small, light weight, handy application you definitely don't want to go with a spring boot application. Spring-boot apps are known to have a delayed startup and leaves you with a bulky jar.
I searched for a light-weight configuration option which has the ease of Spring but not bulky or slow. I couldn't find any so I wrote one. Here's a simple implementation with the most basic features
- To YAML from a file with the help of SnakeYAML
- To access the keys using the dotted notiation.
Get it
This project is hosted in GitHub and artifacts are available in maven repository. You can include this in your maven project as:
<dependency>
<groupId>com.github.jsixface</groupId>
<artifactId>yamlconfig</artifactId>
<version>1.0</version>
</dependency>
Usage
Let's take an example config.yml
as below:
services:
db:
image: mysql
container_name: mysql_db
endpoint:
- host: example.com
port: 2976
- host: example.com
port: 2978
Now to load it into code, pass a reader or an inputstream of the config file to the constructor of YamlConfig
.
InputStream resource = getClass()
.getClassLoader()
.getResourceAsStream("config.yml");
YamlConfig config = YamlConfig.load(resource);
Once you have an instance, you can access the values by dotted notation. You can even access array elements.
String imgName = config.getString("services.db.image");
String host2 = config.getString("services.endpoint[1].host");
Future
Even though the functionality is very basic, I believe this will suffice for a small projects. The future versions will have some more nice features like overriding using environment variables and system properties.