@Target(value={PARAMETER,FIELD,METHOD,CONSTRUCTOR,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented @Qualifier public @interface ConfigProperty
This Qualifier allows to use the DeltaSpike configuration mechanism via simple injection.
Example 1:
@Inject @ConfigProperty(name="locationId") private String locationId;
Example 2 (the type-safe alternative):
@Target({ FIELD, METHOD })
@Retention(RUNTIME)
@ConfigProperty(name = "locationId")
// alternative to null check in the producer:
// @ConfigProperty(name = "locationId", defaultValue = "LOCATION_X")
@Qualifier
public @interface Location
{
}
Depending on the producer it's possible to use a String or a custom type like an enum at the injection point.
With a String:
@Location private String locationId;With a custom type:
@Inject @Location private LocationId locationId;
In any case a custom producer is needed.
BaseConfigPropertyProducer can be used as an base for custom
producers.
Producer for the configured String:
@ApplicationScoped
public class CustomConfigPropertyProducer extends BaseConfigPropertyProducer
{
@Produces
@Dependent
@Location
public String produceLocationId(InjectionPoint injectionPoint)
{
String configuredValue = getStringPropertyValue(injectionPoint);
if (configuredValue == null)
{
return null;
}
return configuredValue;
}
}
Producer for a custom type:
@ApplicationScoped
public class CustomConfigPropertyProducer extends BaseConfigPropertyProducer
{
@Produces
@Dependent
@Location
public LocationId produceLocationId(InjectionPoint injectionPoint)
{
String configuredValue = getStringPropertyValue(injectionPoint);
if (configuredValue == null)
{
return null;
}
return LocationId.valueOf(configuredValue.trim().toUpperCase());
}
}
ConfigResolver,
BaseConfigPropertyProducer| Modifier and Type | Required Element and Description |
|---|---|
String |
name
Name/key of the property
|
| Modifier and Type | Optional Element and Description |
|---|---|
String |
defaultValue
Optional default value.
|
public abstract String name
public abstract String defaultValue
Copyright © 2011-2013 The Apache Software Foundation. All Rights Reserved.