Properties Manager
A container agnostic tool for application wide configuration through properties files.
Features
- Hot reloading
- Multiple source files aggregation
- Automatic property type conversion
- Declarative + programmatic API
- Properties mapping generator
Dependency
<dependency>
<groupId>io.github.thingersoft</groupId>
<artifactId>properties-manager-api</artifactId>
<version>LATEST</version>
</dependency>
Usage
Properties manager offers both declarative and programmatic APIs that can also be mixed together.
For the following examples we'll suppose to deal with this properties file:
sample.string = xxx
sample.integer = 2
sample.date = 01/01/1970
Declarative API
Configure the Properties Manager maven plugin:
<plugin>
<groupId>io.github.thingersoft</groupId>
<artifactId>properties-manager-maven-plugin</artifactId>
<version>LATEST</version>
<executions>
<execution>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
Create a mapping class:
@Properties(propertiesLocations = { "{sample.config.dir}/sample.properties" }, datePattern = "dd/MM/yyyy")
public class SampleProperties {
@Property("sample.string")
public static String sampleString;
@Property("sample.integer")
public static Integer sampleInteger;
@Property("sample.date")
public static Date sampleDate;
}
And that's it, the @Property annotated static fields will get injected with up to date properties values.
The @Properties type level annotation attributes can be used for configuration.
Automatic mapping class generation
The Properties Manager maven plugin also features automatic mapping through the "generate" goal:
<plugin>
<groupId>io.github.thingersoft</groupId>
<artifactId>properties-manager-maven-plugin</artifactId>
<version>LATEST</version>
<executions>
<execution>
<id>enhance</id>
<goals>
<goal>enhance</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<basePackage>com.sample</basePackage>
<propertiesLocations>
<propertiesLocation>{sample.config.dir}/sample.properties</propertiesLocation>
</propertiesLocations>
<templateFiles>
<templateFile>${project.parent.basedir}/config/sample.properties</templateFile>
</templateFiles>
</configuration>
</execution>
</executions>
</plugin>
The above configuration will generate the following class and add it to your sources:
@Properties(
propertiesLocations = { "{sample.config.dir}/sample.properties" },
hotReload = true,
datePattern = "dd/MM/yy H.mm",
locale = "en_US",
obfuscatedPropertyPattern = "",
obfuscatedPropertyPlaceholder = "******"
)
public class ApplicationProperties {
@Property("sample.string")
public static String sampleString;
@Property("sample.integer")
public static String sampleInteger;
@Property("sample.date")
public static String sampleDate;
}
By default the generator will map properties to String fields, whose name will be inferred by converting property keys into camel case.
You can customize mapping behaviour and overall options through plugin configuration:
<plugin>
<groupId>io.github.thingersoft</groupId>
<artifactId>properties-manager-maven-plugin</artifactId>
<version>LATEST</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<basePackage>com.sample</basePackage>
<propertiesLocations>
<propertiesLocation>{sample.config.dir}/sample.properties</propertiesLocation>
</propertiesLocations>
<templateFiles>
<templateFile>${project.parent.basedir}/config/sample.properties</templateFile>
</templateFiles>
<options>
<datePattern>dd/MM/yyyy</datePattern>
</options>
<fieldMappings>
<fieldMapping>
<fieldName>customDateField</fieldName>
<fieldtype>DATE</fieldtype>
<propertyKey>sample.date</propertyKey>
</fieldMapping>
</fieldMappings>
</configuration>
</execution>
<execution>
<id>enhance</id>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
The above configuration will generate:
@Properties(
propertiesLocations = { "{sample.config.dir}/sample.properties" },
hotReload = true,
datePattern = "dd/MM/yyyy",
locale = "en_US",
obfuscatedPropertyPattern = "",
obfuscatedPropertyPlaceholder = "******"
)
public class SampleProperties {
@Property("sample.string")
public static String sampleString;
@Property("sample.integer")
public static String sampleInteger;
@Property("sample.date")
public static Date customDateField;
}
Programmatic API
PropertiesStore.getOptions().setDatePattern("dd/MM/yyyy");
PropertiesStore.loadProperties("etc/sample.properties");
String stringProperty = PropertiesStore.getProperty("sample.string");
Date dateProperty = PropertiesStore.getDate("sample.date");
See javadocs for more details and available options.