@ProviderType public interface Config
Resolves the property value by searching through all configured
ConfigSources
. If the same property is specified in multiple
ConfigSources
, the value in the ConfigSource
with the highest
ordinal will be used.
If multiple ConfigSources
are specified with
the same ordinal, the ConfigSource.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 the registered Converter
which has the highest @javax.annotation.Priority
is used to convert the string value retrieved from the config sources.
ConfigProvider
:
public void doSomething( Config cfg = ConfigProvider.getConfig(); String archiveUrl = cfg.getValue("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)
and getOptionalValue(String, Class)
for accessing a configured value.
Configured values can also be accessed via injection.
See ConfigProperty
for more information.
Modifier and Type | Method and Description |
---|---|
<T> T |
getConfigProperties(Class<T> configProperties)
Return the resolved configuration properties instance.
|
<T> T |
getConfigProperties(Class<T> configProperties,
String prefix)
Return the resolved configuration properties instance with the specified prefix.
|
Iterable<ConfigSource> |
getConfigSources()
Return all of the currently registered configuration sources for this configuration.
|
ConfigValue |
getConfigValue(String propertyName)
Return the
ConfigValue for the specified property name from the underlying
configuration source. |
<T> Optional<Converter<T>> |
getConverter(Class<T> forType)
Return the
Converter used by this instance to produce instances of the specified type from string values. |
<T> Optional<T> |
getOptionalValue(String propertyName,
Class<T> propertyType)
Return the resolved property value with the specified type for the
specified property name from the underlying configuration sources.
|
default <T> Optional<List<T>> |
getOptionalValues(String propertyName,
Class<T> propertyType)
Return the resolved property values with the specified type for the
specified property name from the underlying configuration sources.
|
Iterable<String> |
getPropertyNames()
Returns a sequence of configuration property names.
|
<T> T |
getValue(String propertyName,
Class<T> propertyType)
Return the resolved property value with the specified type for the
specified property name from the underlying configuration sources.
|
default <T> List<T> |
getValues(String propertyName,
Class<T> propertyType)
Return the resolved property values with the specified type for the
specified property name from the underlying configuration sources.
|
<T> T |
unwrap(Class<T> type)
Returns an instance of the specific class, to allow access to the provider specific API.
|
<T> T getValue(String propertyName, Class<T> propertyType)
The configuration value is not guaranteed to be cached by the implementation, and may be expensive to compute; therefore, if the returned value is intended to be frequently used, callers should consider storing rather than recomputing it.
T
- The property typepropertyName
- The configuration property namepropertyType
- The type into which the resolved property value should get convertedIllegalArgumentException
- if the property cannot be converted to the specified typeNoSuchElementException
- if the property isn't present in the configurationConfigValue getConfigValue(String propertyName)
ConfigValue
for the specified property name from the underlying
configuration source. The lookup of the configuration is performed immediatily,
meaning that calls to ConfigValue
will always yeld the same results.
The configuration value is not guaranteed to be cached by the implementation, and may be expensive to compute; therefore, if the returned value is intended to be frequently used, callers should consider storing rather than recomputing it.
A ConfigValue
is always returned even if a property name cannot be found. In this case, every method in
ConfigValue
returns null
except for ConfigValue.getName()
, which includes the original
property name being looked up.
propertyName
- The configuration property nameConfigValue
default <T> List<T> getValues(String propertyName, Class<T> propertyType)
The configuration values are not guaranteed to be cached by the implementation, and may be expensive to compute; therefore, if the returned values are intended to be frequently used, callers should consider storing rather than recomputing them.
T
- The property typepropertyName
- The configuration property namepropertyType
- The type into which the resolved property values should get convertedIllegalArgumentException
- if the property values cannot be converted to the specified typeNoSuchElementException
- if the property isn't present in the configuration<T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType)
The configuration value is not guaranteed to be cached by the implementation, and may be expensive to compute; therefore, if the returned value is intended to be frequently used, callers should consider storing rather than recomputing it. If this method is used very often then consider to locally store the configured value.
T
- The property typepropertyName
- The configuration property namepropertyType
- The type into which the resolved property value should be convertedOptional
wrapping the requested typeIllegalArgumentException
- if the property cannot be converted to the specified typedefault <T> Optional<List<T>> getOptionalValues(String propertyName, Class<T> propertyType)
The configuration values are not guaranteed to be cached by the implementation, and may be expensive to compute; therefore, if the returned values are intended to be frequently used, callers should consider storing rather than recomputing them.
T
- The property typepropertyName
- The configuration property namepropertyType
- The type into which the resolved property values should be convertedOptional
wrapping a list of the requested typeIllegalArgumentException
- if the property cannot be converted to the specified type<T> T getConfigProperties(Class<T> configProperties, String prefix)
T
- The Class TypeconfigProperties
- The class that contains a number of fields that maps to corresponding configuration properties.
The type declaration can be annotated with
@ConfigProperties.
The prefix as the method parameter overrides the prefix set on the type.
This class should contain a zero-arg constructor. Otherwise, non-portable behaviour occurs.prefix
- The prefix for the configuration properties declared on the class configProperties.
If the prefix is "", which means no prefix involved when performing property lookup.
If the prefix is null, this method is equivalent to getConfigProperties(Class).<T> T getConfigProperties(Class<T> configProperties)
T
- The Class TypeconfigProperties
- The class that contains a number of fields that maps to corresponding configuration properties.
The type declaration can be annotated with
@ConfigProperties.
If the type is annotated with @ConfigProperties,
the prefix on the annotation @ConfigProperties
will be honoured.
The absence of the annotation @ConfigProperties
or the absence of the prefix on the annotation means no prefix specified.
This class should contain a zero-arg constructor. Otherwise, non-portable behaviour occurs.Iterable<String> getPropertyNames()
The returned property names are unique; that is, if a name is returned once by a given iteration, it will not be returned again during that same iteration.
There is no guarantee about the completeness or currency of the names returned, nor is there any guarantee that a name that is returned by the iterator will resolve to a non-empty value or be found in any configuration source associated with the configuration; for example, it is allowed for this method to return an empty set always. However, the implementation should return a set of names that is useful to a user that wishes to browse the configuration.
It is implementation-defined whether the returned names reflect a point-in-time "snapshot" of names, or an
aggregation of multiple point-in-time "snapshots", or a more dynamic view of the available property names.
Implementations are not required to return the same sequence of names on each iteration; however, the produced
Iterator
must adhere to the contract of that class, and must not return any more
elements once its hasNext()
method returns false
.
The returned instance is thread safe and may be iterated concurrently. The individual iterators are not thread-safe.
Iterable<ConfigSource> getConfigSources()
<T> Optional<Converter<T>> getConverter(Class<T> forType)
Converter
used by this instance to produce instances of the specified type from string values.T
- the conversion typeforType
- the type to be produced by the converterOptional
containing the converter, or empty if no converter is available for the specified type<T> T unwrap(Class<T> type)
If the MP Config provider implementation does not support the specified class, a IllegalArgumentException
is thrown.
Unwraping to the provider specific API may lead to non-portable behaviour.
T
- The type to unwrap totype
- Class representing the type to unwrap toIllegalArgumentException
- If the current provider does not support unwrapping to the given typeCopyright © 2020 Eclipse Foundation. All rights reserved.