Class Config
The "Default" configuration automatically loads from known files and resources and
is available via asConfiguration(). The methods on Config are convenience
methods that use the underlying "Default" configuration.
Refer to config loading for details on the "Default" configuration sources which include:
- - application.properties invalid input: '&' application.yaml resources invalid input: '&' files
- - system property load.properties
- - command line arguments
- - plugins (like the AWS AppConfig plugin)
Configuration can also be built giving full control - refer to Configuration.builder().
The application can register onChange listeners to handle changes to configuration properties at runtime. Plugins or code can dynamically load and change properties and this can fire any registered callback handlers.
Obtain the "default" configuration
Configuration defaultConfiguration = Config.asConfiguration();
"Default" configuration usage examples
int port = Config.getInt("app.port", 8090);
String topicName = Config.get("app.topic.name");
List<Integer> codes = Config.list().ofInt("my.codes", 42, 54);
Build a configuration
var configuration = Configuration.builder()
.put("myKey", "myValue")
.load(someFile) // load a yaml or properties file
.load(someResource) // load a yaml or properties resource from the classpath
.build();
Use Configuration
Configuration defaultConfiguration = Config.asConfiguration();
int port = defaultConfiguration.getInt("port", 8080);
String host = defaultConfiguration.get("host", "localhost");
-
Method Summary
Modifier and TypeMethodDescriptionstatic ConfigurationReturn the underlying configuration.static PropertiesReturn the loaded properties as standard Properties map.static voidclearProperty(String key) Clear the value for the given key.static booleanReturn boolean configuration value with the given default value.static booleanReturn boolean configuration value with the given default value.static ModificationEvent.BuildereventBuilder(String name) Create an event builder to make changes to the configuration.static ConfigurationReturn the configuration for a path.static StringReturn a required configuration value as String.static StringReturn a configuration string value with a given default.static <T> TApply a mapping function to the value returned.static <T> Optional<T> getAsOptional(String key, Function<String, T> mappingFunction) Apply a mapping function to the value returned as an optional.static booleanReturn a required boolean configuration value.static booleanReturn a configuration value as boolean given a default value.static BigDecimalgetDecimal(String key) Return a decimal configuration value.static BigDecimalgetDecimal(String key, String defaultValue) Return a decimal configuration value with a default value.static DurationgetDuration(String key) Return a Duration configuration value.static DurationgetDuration(String key, String defaultValue) Return a Duration configuration value with a default value.static <T extends Enum<T>>
TReturn the enum configuration value.static <T extends Enum<T>>
TReturn the enum configuration value with a default value.static intReturn a required int configuration value.static intReturn a configuration value as int given a default value.static longReturn a required long configuration value.static longReturn a configuration value as long given a default value.static @Nullable StringgetNullable(String key) Return a configuration value as String or null if it is not defined.static @Nullable StringgetNullable(String key, @Nullable String defaultValue) Return a configuration value as String or null if it is not defined.getOptional(String key) Return a configuration value that might not exist.getOptional(String key, @Nullable String defaultValue) Return a configuration value that might not exist.static URIReturn a URI configuration value.static URIReturn a URI configuration value with a default value.static Configuration.ListValuelist()Return a List of values configured.static voidPut all loaded properties into System properties.static voidRegister a callback for a change to the given configuration key.static voidonChange(Consumer<ModificationEvent> bulkChangeEventListener, String... keys) Register an event listener that will be notified of configuration changes.static voidonChangeBool(String key, Consumer<Boolean> singlePropertyChangeListener) Register a callback for a change to the given configuration key as a Boolean value.static voidonChangeInt(String key, IntConsumer singlePropertyChangeListener) Register a callback for a change to the given configuration key as an Int value.static voidonChangeLong(String key, LongConsumer singlePropertyChangeListener) Register a callback for a change to the given configuration key as a Long value.static voidAdd configuration values via a map.static Configuration.SetValueset()Return a Set of values configured.static voidsetProperty(String key, String value) Set a single configuration value.
-
Method Details
-
asProperties
Return the loaded properties as standard Properties map. -
forPath
Return the configuration for a path.Examples
Say you have a set of properties like this
example.path.prefix1=1
example.path.prefix2=2
Configuration config = Config.forPath("example.path"); Configuration config2 = Config.forPath("example").forPath("path"); // will output "1" int number = config.getInt("prefix1"); // will output "2" number = config2.getInt("prefix2"); -
asConfiguration
Return the underlying configuration. -
loadIntoSystemProperties
public static void loadIntoSystemProperties()Put all loaded properties into System properties. -
get
-
get
-
getOptional
-
getOptional
-
getNullable
Return a configuration value as String or null if it is not defined.This is an alternative to
getOptional(String)for cases where we prefer to work with null values rather than Optional.- Parameters:
key- The configuration key- Returns:
- The configured value or null if not set
-
getNullable
Return a configuration value as String or null if it is not defined.This is an alternative to
getOptional(String)for cases where we prefer to work with null values rather than Optional.- Parameters:
key- The configuration keydefaultValue- The default value that can be null- Returns:
- The configured value or null if not set
-
enabled
Return boolean configuration value with the given default value.This is the same as
getBool(String).IllegalStateException is thrown if the value is not defined in configuration.
if (Config.enabled("feature.cleanup")) { ... }- Parameters:
key- The configuration key- Returns:
- True when configuration value is true
-
enabled
Return boolean configuration value with the given default value.This is the same as
getBool(String, boolean).if (Config.enabled("feature.cleanup", true)) { ... }- Parameters:
key- The configuration key- Returns:
- True when configuration value is true
-
getBool
Return a required boolean configuration value.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
key- The configuration key- Returns:
- The configured value
-
getBool
Return a configuration value as boolean given a default value.- Parameters:
key- The configuration keydefaultValue- The default value used- Returns:
- The configured or default value
-
getInt
Return a required int configuration value.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
key- The configuration key- Returns:
- The configured value
-
getInt
Return a configuration value as int given a default value.- Parameters:
key- The configuration keydefaultValue- The default value used- Returns:
- The configured or default value
-
getLong
Return a required long configuration value.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
key- The configuration key- Returns:
- The configured value
-
getLong
Return a configuration value as long given a default value.- Parameters:
key- The configuration keydefaultValue- The default value used- Returns:
- The configured or default value
-
getDecimal
Return a decimal configuration value.- Parameters:
key- The configuration key- Returns:
- The configured value
-
getDecimal
Return a decimal configuration value with a default value.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
key- The configuration keydefaultValue- The default value- Returns:
- The configured value
-
getURI
-
getURI
-
getDuration
-
getDuration
-
getEnum
-
getEnum
-
getAs
-
getAsOptional
-
list
Return a List of values configured.List<Integer> codes = Config.list().ofInt("my.codes", 97, 45); -
set
Return a Set of values configured.Set<String> operations = Config.set().of("my.operations", "put","delete"); -
eventBuilder
Create an event builder to make changes to the configuration.configuration.eventBuilder("MyChanges") .put("someKey", "val0") .put("someOther.key", "42") .remove("foo") .publish();- Parameters:
name- The name of the event which defines the source of the configuration value.- See Also:
-
setProperty
Set a single configuration value. Note thateventBuilder(String)should be used to fluently set multiple configuration values.This will fire configuration callback listeners that are registered.
-
putAll
-
clearProperty
Clear the value for the given key. Note thateventBuilder(String)should be used when setting multiple configuration values.This will fire configuration callback listeners that are registered.
- Parameters:
key- The configuration key we want to clear
-
onChange
Register an event listener that will be notified of configuration changes.If we are only interested in changes to a single property it is easier to use
onChange(String, Consumer)or the variants for int, long, boolean onChangeInt(), onChangeLong(), onChangeBool().Typically, we use this when we are interested in changes to multiple properties and want to get and act on the values of multiple properties.
configuration.onChange((modificationEvent) -> { String newValue = modificationEvent.configuration().get("myFirstKey"); int newInt = modificationEvent.configuration().getInt("myOtherKey"); // do something ... });When we are only interested if some specific properties have changed then we can define those. The event listener will be invoked if there is a change to any of those keys.
configuration.onChange((event) -> { String newValue = event.configuration().get("myFirstInterestingKey"); int newInt = event.configuration().getInt("myOtherInterestingKey"); // do something ... }, "myFirstInterestingKey", "myOtherInterestingKey");- Parameters:
bulkChangeEventListener- The listener that is called when changes have occurredkeys- Optionally specify keys when the listener is only interested if changes are made for these specific properties
-
onChange
Register a callback for a change to the given configuration key.Use this when we are only interested in changes to a single configuration property. If we are interested in multiple properties we should use
onChange(Consumer, String...)configuration.onChange("myKey", (newValue) -> { // do something with the newValue ... )};- Parameters:
key- The configuration key we want to detect changes tosinglePropertyChangeListener- The callback handling to fire when the configuration changes.
-
onChangeInt
Register a callback for a change to the given configuration key as an Int value.Use this when we are only interested in changes to a single configuration property. If we are interested in multiple properties we should use
onChange(Consumer, String...)- Parameters:
key- The configuration key we want to detect changes tosinglePropertyChangeListener- The callback handling to fire when the configuration changes.
-
onChangeLong
Register a callback for a change to the given configuration key as a Long value.Use this when we are only interested in changes to a single configuration property. If we are interested in multiple properties we should use
onChange(Consumer, String...)- Parameters:
key- The configuration key we want to detect changes tosinglePropertyChangeListener- The callback handling to fire when the configuration changes.
-
onChangeBool
Register a callback for a change to the given configuration key as a Boolean value.Use this when we are only interested in changes to a single configuration property. If we are interested in multiple properties we should use
onChange(Consumer, String...)- Parameters:
key- The configuration key we want to detect changes tosinglePropertyChangeListener- The callback handling to fire when the configuration changes.
-