config-java
Automatic Configuration Reload Java
I wanted to have a pure Java solution (no dependency) for typed configurations, which can be reloaded when the configuration on disk changes, and needs to work with Java 8. Also, it should be like a C/C++ header only file. You place it in your project, modify the package and it’s ready to use. No jar-file, no dependency. Could be simpler, but when I look back in 6 months I find that all my code looks terrible no matter how much SOLID/Clean-Code I use. The interface must only be used for configuration. All methods must have no parameters and only String, double, long, int, boolean are allowed as return types.
My Requirements
Dynamic Configuration Reloading and Type Safety:
Configurations are dynamically reloaded when the underlying property files change, ensuring the application always has the latest configuration without requiring a restart.
The use of interfaces with annotated methods allows for compile-time type safety.
Invalid configurations (wrong types or parameters) are caught early, reducing runtime errors.
Alternatively, you could use an implementation of MicroProfile Config. However, at that time, there was no simple reload feature available, and it would have introduced another dependency.
package de.codecoverage.grpc.impl;
public class Worker {
@Configurable(filePath = "masterconfiguration.properties")
public interface Config {
@ConfigurationValue(key = "de.codecoverage.grpc.impl.Worker.dumpClientResponse", defaultValue = "false")
boolean logClientResponse ();
@ConfigurationValue(key = "de.codecoverage.grpc.impl.Worker.logBufferSize", defaultValue = "1024")
int getLogBufferSize();
}
If “filePath” is within “@” symbols, the string is treated as a Java system property. Useful in cases where you want to control it from outside when the JVM starts:
@Configurable(filePath = "@SYSTEM_PROPERTY_CONFIG@")
public interface TESTConfigProperty {
@ConfigurationValue(key = "codecoverage.de.config.test_string", defaultValue = "a simple string, must be the same as default")
String getString();
}