The main goal of this project is to explore basic features of
com.typesafe:config
.
Reference: https://github.com/lightbend/config#overview
com.typesafe:config
is a type-safe configuration library for
JVM languages.
All we need to do to start using com.typesafe:config
is:
- add dependency
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.3</version>
</dependency>
- add
application.conf
toresources
- get
Config
class
Config conf = ConfigFactory.load();
test/resources/application.conf
- file with most popular use-cases. It's written in HOCON.test/java/ConfigTest
- we test if all lines fromapplication.conf
file was loaded successfully.test/java/Author
- util class for mapping specific entry inapplication.conf
directly to theJavaBean
.test/java/UserStatus
- util class for mapping specific entry inapplication.conf
directly to theEnum
.
- Assumptions:
Config conf = ConfigFactory.load();
application.conf
has structure:predefined { ... } conf { ... }
- Basic example
conf { project_name = typesafe-config }
conf.getString("conf.project_name") // typesafe-config
- Substitutions (way of eliminating copy-paste)
predefined { version : 1.0-SNAPSHOT } conf { project_version : ${predefined.version} artifact_version : ${predefined.version} }
Note:conf.getString("conf.project_version") // 1.0-SNAPSHOT conf.getString("conf.artifact_version") // 1.0-SNAPSHOT
Remember if there is no definedyyy
thenxxx : ${yyy}
will causeExceptionInInitializerError: Could not resolve substitution to a value: ${yyy}
- Handling
JSON
objectsauthor : {name : michal, surname : tumilowicz}
ConfigObject author = conf.getObject("conf.author"); Config asConfig = author.toConfig(); // treat it as a Config asConfig.getString("name"); // michal asConfig.getString("surname"); // tumilowicz
- Mapping
JSON
toJavaBean
If you have aJava
object that followsJavaBean
conventions (zero-args constructor, getters and setters), you can automatically initialize it from aConfig
.author : {name : michal, surname : tumilowicz}
Author author = ConfigBeanFactory.create(conf.getObject("conf.author").toConfig(), Author.class); author.getName(); // michal author.getSurname(); // tumilowicz
- Merging
- Values on the same line are concatenated (for strings and arrays) or merged (for objects).
- If you duplicate a field with an object value, then the objects are merged with last-one-wins.
is merged to:persistence : {specification: JPA, provider : Hibernate, cache : true} persistence : {provider : EclipseLink, cache : false, database : Oracle}
persistence : {specification: JPA, provider : EclipseLink, cache : false, database : Oracle}
- Merging + substitution
Merging is especially useful with substitutions.is merged to:predefined { headquarters : {name : "mtumilowicz holding"} } conf { branch_east : ${predefined.headquarters} {branch_name : east} }
headquarters : {name : "mtumilowicz holding", branch_name : east}
- Substitutions with default value
Here, the field
predefined { } conf { web_container : GlassFish web_container : ${?predefined.web_container} }
web_container : ${?predefined.web_container}
simply vanishes if there's no value forpredefined.web_container
. But if you setpredefined.web_container
- it would be used.Contrary:conf.getString("conf.web_container") // GlassFish
predefined { login: admin ] conf { login : ${?predefined.login} }
conf.getString("conf.login") // admin
- Lists
languages : [english, polish, french]
conf.getStringList("conf.languages") // [english, polish, french]
- Enums
user_status : [PENDING, ACTIVE, INACTIVE, DELETED]
public enum UserStatus { PENDING, ACTIVE, INACTIVE, DELETED }
conf.getEnumList(UserStatus.class, "conf.user_status") // [PENDING, ACTIVE, INACTIVE, DELETED]