Annotation Type ConfigProperty
-
@Qualifier @Retention(RUNTIME) @Target({METHOD,FIELD,PARAMETER,TYPE}) public @interface ConfigProperty
Binds the injection point with a configured value. Can be used to annotate injection points of type
TYPE
,Optional<TYPE>
orjavax.inject.Provider<TYPE>
, whereTYPE
can beString
and all types which have appropriate converters.Injected values are the same values that would be retrieved from an injected
Config
instance or from the instance retrieved fromConfigProvider.getConfig()
Examples
Injecting Native Values
The first sample injects the configured value of themy.long.property
property. The injected value does not change even if the underline property value changes in theConfig
.Injecting a native value is recommended for a mandatory property and its value does not change at runtime or used by a bean with RequestScoped.
A further recommendation is to use the built in
META-INF/microprofile-config.properties
file mechanism to provide default values inside an Application. If no configured value exists for this property, aDeplymentException
will be thrown during startup.@Inject @ConfigProperty(name="my.long.property") private Long injectedLongValue;
Injecting Optional Values
Contrary to natively injecting, if the property is not specified, this will not lead to a DeploymentException. The following code injects a Long value to themy.optional.long.property
. If the property does not exist, the value123
will be assigned. toinjectedLongValue
.@Inject @ConfigProperty(name="my.optional.long.property", defaultValue="123") private Long injectedLongValue;
The following code injects an Optional value ofmy.optional.int.property
.@Inject @ConfigProperty(name = "my.optional.int.property") private Optional<Integer> intConfigValue;
Injecting Dynamic Values
The next sample injects a Provider for the value ofmy.long.property
property to resolve the property dynamically. Each invocation toProvider#get()
will resolve the latest value from underlyingConfig
again. The existence of configured values will get checked during startup. Instances ofProvider<T>
are guaranteed to be Serializable.@Inject @ConfigProperty(name = "my.long.property" defaultValue="123") private Provider<Long> longConfigValue;
If
ConfigProperty
is used with a type where noConverter
exists, a deployment error will be thrown.
-
-
Field Summary
Fields Modifier and Type Fields Description static java.lang.String
UNCONFIGURED_VALUE
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.String
defaultValue
The default value if the configured property value does not exist.java.lang.String
name
The key of the config property used to look up the configuration value.
-
-
-
Element Detail
-
name
java.lang.String name
The key of the config property used to look up the configuration value. If it is not specified, it will be derived automatically as<class_name>.<injetion_point_name>
, whereinjection_point_name
is the field name or parameter name,class_name
is the fully qualified name of the class being injected to. If one of theclass_name
orinjection_point_name
cannot be determined, the value has to be provided.- Returns:
- Name (key) of the config property to inject
- Default:
- ""
-
-
-
defaultValue
java.lang.String defaultValue
The default value if the configured property value does not exist.
If the target Type is not String a proper
Converter
will get applied. That means that any default value string should follow the formatting rules of the registered Converters.- Returns:
- the default value as a string
- Default:
- "org.eclipse.microprofile.config.configproperty.unconfigureddvalue"
-
-