Class Config
- java.lang.Object
-
- io.avaje.config.Config
-
public class Config extends Object
Provides application Configuration based on loading properties and yaml files as well as plugins that supply properties (like dynamic configuration loaded from a db).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.
Examples
int port = Config.getInt("app.port", 8090); String topicName = Config.get("app.topic.name"); List<Integer> codes = Config.getList().ofInt("my.codes", 42, 54);
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ConfigurationasConfiguration()Return the underlying configuration.static PropertiesasProperties()Return the loaded properties as standard Properties map.static booleanenabled(String key)Return boolean configuration value with the given default value.static booleanenabled(String key, boolean enabledDefault)Return boolean configuration value with the given default value.static Stringget(String key)Return a required configuration value as String.static Stringget(String key, String defaultValue)Return a configuration string value with a given default.static booleangetBool(String key)Return a required boolean configuration value.static booleangetBool(String key, boolean defaultValue)Return 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 <T extends Enum<T>>
TgetEnum(Class<T> type, String key)Return the enum configuration value.static <T extends Enum<T>>
TgetEnum(Class<T> type, String key, T defaultValue)Return the enum configuration value with a default value.static intgetInt(String key)Return a required int configuration value.static intgetInt(String key, int defaultValue)Return a configuration value as int given a default value.static Configuration.ListValuegetList()Return a List of values configured.static longgetLong(String key)Return a required long configuration value.static longgetLong(String key, long defaultValue)Return a configuration value as long given a default value.static Optional<String>getOptional(String key)Return a configuration value that might not exist.static Configuration.SetValuegetSet()Return a Set of values configured.static URLgetURL(String key)Return a URL configuration value.static URLgetURL(String key, String defaultValue)Return a URL configuration value with a default value.static voidloadIntoSystemProperties()Put all loaded properties into System properties.static voidonChange(String key, Consumer<String> callback)Register a callback for a change to the given configuration key.static voidonChangeBool(String key, Consumer<Boolean> callback)Register a callback for a change to the given configuration key as an Boolean value.static voidonChangeInt(String key, Consumer<Integer> callback)Register a callback for a change to the given configuration key as an Int value.static voidonChangeLong(String key, Consumer<Long> callback)Register a callback for a change to the given configuration key as an Long value.static voidsetProperty(String key, String value)Set a configuration value.
-
-
-
Method Detail
-
asProperties
public static Properties asProperties()
Return the loaded properties as standard Properties map.
-
asConfiguration
public static Configuration asConfiguration()
Return the underlying configuration.
-
loadIntoSystemProperties
public static void loadIntoSystemProperties()
Put all loaded properties into System properties.
-
get
public static String get(String key)
Return a required configuration value as String.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
key- The configuration key- Returns:
- The configured value
-
get
public static String get(String key, String defaultValue)
Return a configuration string value with a given default.- Parameters:
key- The configuration keydefaultValue- The default value used- Returns:
- The configured or default value
-
getOptional
public static Optional<String> getOptional(String key)
Return a configuration value that might not exist.- Parameters:
key- The configuration key- Returns:
- The configured value wrapped as optional
-
enabled
public static boolean enabled(String key)
Return boolean configuration value with the given default value.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
public static boolean enabled(String key, boolean enabledDefault)
Return boolean configuration value with the given default value.if (Config.enabled("feature.cleanup", true)) { ... }- Parameters:
key- The configuration key- Returns:
- True when configuration value is true
-
getBool
public static boolean getBool(String key)
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
public static boolean getBool(String key, boolean defaultValue)
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
public static int getInt(String key)
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
public static int getInt(String key, int defaultValue)
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
public static long getLong(String key)
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
public static long getLong(String key, long defaultValue)
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
public static BigDecimal getDecimal(String key)
Return a decimal configuration value.- Parameters:
key- The configuration key- Returns:
- The configured value
-
getDecimal
public static BigDecimal getDecimal(String key, String defaultValue)
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
-
getURL
public static URL getURL(String key)
Return a URL configuration value.- Parameters:
key- The configuration key- Returns:
- The configured value
-
getURL
public static URL getURL(String key, String defaultValue)
Return a URL 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
-
getEnum
public static <T extends Enum<T>> T getEnum(Class<T> type, String key)
Return the enum configuration value.IllegalStateException is thrown if the value is not defined in configuration.
- Parameters:
type- The enum typekey- The configuration key- Returns:
- The configured value
-
getEnum
public static <T extends Enum<T>> T getEnum(Class<T> type, String key, T defaultValue)
Return the enum configuration value with a default value.- Parameters:
type- The enum typekey- The configuration keydefaultValue- The default value- Returns:
- The configured value
-
getList
public static Configuration.ListValue getList()
Return a List of values configured.List<Integer> codes = Config.getList().ofInt("my.codes", 97, 45);
-
getSet
public static Configuration.SetValue getSet()
Return a Set of values configured.Set<String> operations = Config.getSet().of("my.operations", "put","delete");
-
setProperty
public static void setProperty(String key, String value)
Set a configuration value.This will fire an configuration callback listeners that are registered for this key.
-
onChange
public static void onChange(String key, Consumer<String> callback)
Register a callback for a change to the given configuration key.- Parameters:
key- The configuration key we want to detect changes tocallback- The callback handling to fire when the configuration changes.
-
onChangeInt
public static void onChangeInt(String key, Consumer<Integer> callback)
Register a callback for a change to the given configuration key as an Int value.- Parameters:
key- The configuration key we want to detect changes tocallback- The callback handling to fire when the configuration changes.
-
onChangeLong
public static void onChangeLong(String key, Consumer<Long> callback)
Register a callback for a change to the given configuration key as an Long value.- Parameters:
key- The configuration key we want to detect changes tocallback- The callback handling to fire when the configuration changes.
-
onChangeBool
public static void onChangeBool(String key, Consumer<Boolean> callback)
Register a callback for a change to the given configuration key as an Boolean value.- Parameters:
key- The configuration key we want to detect changes tocallback- The callback handling to fire when the configuration changes.
-
-