Top Description Fields Methods
org.eclipse.microprofile.config.inject

public @Interface ConfigProperty

extends Annotation
Annotations
@Qualifier
@Retention:RUNTIME
@Target:METHOD, FIELD, PARAMETER, TYPE
@Requirements:@org.osgi.annotation.bundle.Requirement(namespace="osgi.cdi.extension", name="org.eclipse.microprofile.config", effective="active"), @org.osgi.annotation.bundle.Requirement(namespace="osgi.cdi.extension", name="org.eclipse.microprofile.config", resolution="OPTIONAL")
Static Imports
java.lang.annotation.ElementType.FIELD, .ElementType.METHOD, .ElementType.PARAMETER, .ElementType.TYPE, .RetentionPolicy.RUNTIME, org.osgi.annotation.bundle.Requirement.Resolution.OPTIONAL, org.osgi.service.cdi.CDIConstants.CDI_EXTENSION_PROPERTY

Binds the injection point with a configured value.

Can be used to annotate injection points of type TYPE, Optional<TYPE> or jakarta.inject.Provider<TYPE>, where TYPE can be String and all types which have appropriate converters.

Injected values are the same values that would be retrieved from an injected org.eclipse.microprofile.config.Config instance or from the instance retrieved from org.eclipse.microprofile.config.ConfigProvider#getConfig()

Examples

Injecting Native Values

The first sample injects the configured value of the my.long.property property. The injected value does not change even if the underline property value changes in the org.eclipse.microprofile.config.Config.

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, a DeploymentException 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 the my.optional.long.property. If the property is not defined, the value 123 will be assigned to injectedLongValue.

@Inject
@ConfigProperty(name = "my.optional.long.property", defaultValue = "123")
private Long injectedLongValue;

The following code injects an Optional value of my.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 of my.long.property property to resolve the property dynamically. Each invocation to Provider#get() will resolve the latest value from underlying org.eclipse.microprofile.config.Config again. The existence of configured values will get checked during startup. Instances of Provider<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 no org.eclipse.microprofile.config.spi.Converter exists, a deployment error will be thrown.

Authors
Ondrej Mihalyi, Emily Jiang, Mark Struberg

Field Summary

Modifier and TypeField and Description
public static final String

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the default value as a string
defaultValue
()

The default value if the configured property does not exist.

public String

Returns:

Name (key) of the config property to inject
name
()

The key of the config property used to look up the configuration value.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Field Detail

UNCONFIGURED_VALUEback to summary
public static final String UNCONFIGURED_VALUE

Method Detail

defaultValueback to summary
public String defaultValue()

The default value if the configured property does not exist. This value acts as a configure source with the lowest ordinal.

If the target Type is not String, a proper org.eclipse.microprofile.config.spi.Converter will get applied. Empty string as the default value will be ignored, which is same as not setting the default value. That means that any default value string should follow the formatting rules of the registered Converters. If a property has been emptied by a config source with a higher ordinal by setting an empty configuration value or by using a value causing the used converter returning null, the default value will not be used.

Returns:String

the default value as a string

Annotations
@Nonbinding
nameback to summary
public 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>.<injection_point_name>, where injection_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 the class_name or injection_point_name cannot be determined, the value has to be provided.

Returns:String

Name (key) of the config property to inject

Annotations
@Nonbinding