Dynamic Configuration Capability for SpringBoot Application
Hot-reload your SpringBoot configurations, with just a '@DynamicConfig' annotation, the simplest solution, ever.
If your Spring Boot version is 2.4 or lower, please use version 1.0.8
Maven
<dependency>
<groupId>top.code2life</groupId>
<artifactId>spring-boot-dynamic-config</artifactId>
<version>1.0.9</version>
</dependency>
Gradle
implementation 'top.code2life:spring-boot-dynamic-config:1.0.9'
Option1: Add @DynamicConfig annotation on class which contains @Value field.
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import top.code2life.config.DynamicConfig;
import java.util.Set;
@Data
@Component
@DynamicConfig // add annotation here !
public class DynamicFeatures {
@Value("${dynamic-test-plain:default}")
private String plainValue;
@Value("#{@featureGate.convert('${dynamic-feature-conf}')}")
private Set<String> someBetaFeatureConfig;
// @DynamicConfig // adding annotation here also works!
@Value("#{@testComponent.transform(${dynamic.transform-a:20}, ${dynamic.transform-b:10})} ")
private double transformBySpEL;
public double transform(double t1, double t2) {
return t1 / t2;
}
}
// file: application-profile.yml
// ============================
// dynamic-test-plain: someVal # kebab-case is recommended
// dynamicFeatureConf: a,b,c # camelCase compatible
// dynamic:
// transform-a: 100
// transform-b: 10
Option2: Add @ConfigurationProperties annotation on configuration class.
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import top.code2life.config.DynamicConfig;
import java.util.Map;
@Data
@DynamicConfig // add annotation here !
@Configuration
@ConfigurationProperties(prefix = "my-prop")
public class TestConfigurationProperties {
private String str;
private Double doubleVal;
private Map<String, Object> mapVal;
}
// file: application-another-profile.yml
// ============================
// my-prop: # or myProp, relax binding supported
// str: someVal
// double-val: 100.0
// mapVal:
// k: v
java -jar your-spring-boot-app.jar --spring.config.location=/path/to/config
Since Version 1.0.9 and Spring Boot 2.4, 'spring.config.import' is available !
# config.import could be used with config.location TOGETHER
java -jar your-spring-boot-app.jar --spring.config.import=/path/to/configA.yaml,/path/to/configB.yaml
# or use the config tree feature
java -jar your-spring-boot-app.jar --spring.config.import=configtree:/path/to/conf-dir/
Then, modifications on /path/to/config/application-
When using '--spring.config.import=configtree:/path/to/conf-dir/', Spring Boot will load all files recursively.
It will result in ConfigTreePropertySource in Spring environments, with key of file path, for example: If file is 'path/to/conf-dir/module-a/file-b.yaml', the property key is 'module-a.file-b.yaml', the value is the content of the file.
Dynamic Config version 1.0.9 enhanced this feature, when properties or yaml files found in config tree, it will append additional property sources, and will watch all file changes and then manipulate these property sources to keep them Dynamic, so that you could use like following:
Suppose you have this file in spring.config.import directory '/path/to/conf-dir',
# file: /path/to/conf-dir/module-a/file-b.yaml
prop-c-in-file: example-value
prop-obj-key-infile:
fieldA: value
fieldB: value
Just adding prefix it will work, the prefix is similar to the file's relative path module-a/file-b.yaml => module-a.file-b。
@Value("${module-a.file-b.prop-c-in-file}")
@DynamicConfig
private String loadedByConfigTree
// or
@DynamicConfig
@ConfigurationProperties(prefix = "module-a.file-b.prop-obj-key-infile")
public class PropsLoadedByConfigTree {
// ...
}
Any SpringBoot/SpringCloud application within following SpringBoot version can use this lib.
NOTES:
Spring Boot Dynamic Config is Open Source software released under the Apache 2.0 license.