TBD
- True typesafe configuration
- Strong properties validation
- Possibility to subscribe to properties change
Lets imagine that you are developing a java-based server, which reads currencies from the database and distributes them to your website through Rest service.
You should be able to configure server to work in three environments: SIT, UAT, Prod Also your production servers are located in different countries: Russia, USA and India
You want to configure:
- Database URL
- Language that you want to use for your Rest services response
So, lets get started!
First, create a space configuration file (located: ${project.basedir}/src/main/resources/config-spaces.conf)
space {
env: [sit, uat, prod]
location: [no_location, rus, usa, india]
}
points [
{env: sit, location: no_location},
{env: uat, location: no_location},
{env: prod, location: rus},
{env: prod, location: usa},
{env: prod, location: india},
]
Now, create a configuration file with the values (located: ${project.basedir}/src/main/resources/config.conf)
database.url {
~sit: 'my_sit_database'
~uat: 'my_uat_database'
~prod: 'my_prod_database'
}
language {
~default: 'eng' // ~default - special keyword for default values
~rus: 'russia'
}
<properties>
<smart.config.csvProperties>${project.basedir}/src/main/resources/config.conf</smart.config.csvProperties>
<smart.config.dimensions>${project.basedir}/src/main/resources/config-spaces.conf</smart.config.dimensions>
</properties>
<dependencies>
<dependency>
<groupId>com.github.sergeybudnik</groupId>
<artifactId>smart.config.data</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.github.sergeybudnik</groupId>
<artifactId>smart.config</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>jenesis4java</goal>
</goals>
</execution>
</executions>
<configuration>
<relativeDirectory>/smart-generated-sources/smart-config</relativeDirectory> <!-- Optional -->
<csvProperties>${project.basedir}/src/main/resources/smart-config-test.conf</csvProperties> <!-- Required -->
<dimensions>${project.basedir}/src/main/resources/smart-config-dimensions.conf</dimensions> <!-- Required -->
</configuration>
</plugin>
</plugins>
</build>
Run 'mvn clean install'
After plugin execution, you will be able to work with properties in a following way:
SmartConfig smartConfig = SmartConfigProperties.getConfig("prod", "usa");
String databaseUrl = smartConfig.getDatabaseUrl();
SmartConfigValue<String> databaseUrlConfig = smartConfig.getDatabaseUrlConfig();
System.out.println(databaseUrl); // Prints 'my_prod_database'