Interface Config
-
@ProviderType public interface Config
Resolves the property value by searching through all configured
ConfigSources
. If the same property is specified in multipleConfigSources
, the value in theConfigSource
with the highest ordinal will be used.If multiple
ConfigSources
are specified with the same ordinal, theConfigSource.getName()
will be used for sorting.The config objects produced via the injection model
@Inject Config
are guaranteed to be serializable, while the programmatically created ones are not required to be serializable.If one or more converters are registered for a class of a requested value then one of the registered converters which has the highest priority is used to convert the string value retrieved from config sources. The highest priority means the highest priority number. For more information about converters, see
Converter
Usage
For accessing the config you can use theConfigProvider
:public void doSomething( Config cfg = ConfigProvider.getConfig(); String archiveUrl = cfg.getString("my.project.archive.endpoint", String.class); Integer archivePort = cfg.getValue("my.project.archive.port", Integer.class);
It is also possible to inject the Config if a DI container is available:
public class MyService { @Inject private Config config; }
See
getValue(String, Class)
andgetOptionalValue(String, Class)
for accessing a configured value.Configured values can also be accessed via injection. See
ConfigProperty
for more information.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.Iterable<ConfigSource>
getConfigSources()
<T> java.util.Optional<T>
getOptionalValue(java.lang.String propertyName, java.lang.Class<T> propertyType)
Return the resolved property value with the specified type for the specified property name from the underlyingConfigSources
.java.lang.Iterable<java.lang.String>
getPropertyNames()
Return a collection of property names.<T> T
getValue(java.lang.String propertyName, java.lang.Class<T> propertyType)
Return the resolved property value with the specified type for the specified property name from the underlyingConfigSources
.
-
-
-
Method Detail
-
getValue
<T> T getValue(java.lang.String propertyName, java.lang.Class<T> propertyType)
Return the resolved property value with the specified type for the specified property name from the underlyingConfigSources
. If this method gets used very often then consider to locally store the configured value.- Type Parameters:
T
- The property type- Parameters:
propertyName
- The configuration propertyName.propertyType
- The type into which the resolve property value should get converted- Returns:
- the resolved property value as an object of the requested type.
- Throws:
java.lang.IllegalArgumentException
- if the property cannot be converted to the specified type.java.util.NoSuchElementException
- if the property isn't present in the configuration.
-
getOptionalValue
<T> java.util.Optional<T> getOptionalValue(java.lang.String propertyName, java.lang.Class<T> propertyType)
Return the resolved property value with the specified type for the specified property name from the underlyingConfigSources
. If this method is used very often then consider to locally store the configured value.- Type Parameters:
T
- The property type- Parameters:
propertyName
- The configuration propertyName.propertyType
- The type into which the resolve property value should be converted- Returns:
- The resolved property value as an Optional of the requested type.
- Throws:
java.lang.IllegalArgumentException
- if the property cannot be converted to the specified type.
-
getPropertyNames
java.lang.Iterable<java.lang.String> getPropertyNames()
Return a collection of property names.- Returns:
- the names of all configured keys of the underlying configuration.
-
getConfigSources
java.lang.Iterable<ConfigSource> getConfigSources()
- Returns:
- all currently registered
configsources
sorted with descending ordinal and ConfigSource name
-
-