public abstract class BaseConfigPropertyProducer extends Object
This contains the fundamental parts for implementing own ConfigProperty producers.
DeltaSpikes own configuration system only natively only supports Strings. If you like to apply own Converters or extract other types from those Strings, you can simply do this by providing an own Qualifier and a simple CDI producer method for it.
First we write a simple Qualifier:
@Target({ PARAMETER, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE }) @Retention(RUNTIME) @ConfigProperty(named="unused") // the name @Qualifier public @interface NumberConfig { @Nonbinding boolean name(); // the name of the configuration-key to lookup the value @Nonbinding String defaultValue() default ConfigProperty.NULL; @Nonbinding boolean pattern(); // the pattern for NumberFormatter }
The producer method implementation is pretty easy as well:
@ApplicationScoped public class NumberConfigProducer extends BaseConfigPropertyProducer { @Produces @Dependent @NumberConfig public Float produceNumberConfig(InjectionPoint injectionPoint) { // resolve the annotation NumberConfig metaData = getAnnotation(injectionPoint, NumberConfig.class); // get the configured value from the underlying configuration system String configuredValue = getPropertyValue(metaData.name(), metaData.defaultValue()); if (configuredValue == null) { return null; } // format according to the given pattern DecimalFormat df = new DecimalFormat(metaData.pattern(), new DecimalFormatSymbols(Locale.US)); return df.parse(configuredValue).floatValue(); * } }
Constructor and Description |
---|
BaseConfigPropertyProducer() |
Modifier and Type | Method and Description |
---|---|
protected <T extends java.lang.annotation.Annotation> |
getAnnotation(javax.enterprise.inject.spi.InjectionPoint injectionPoint,
Class<T> targetType) |
protected String |
getPropertyValue(String propertyName,
String defaultValue) |
protected String |
getStringPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint)
Inspects the given InjectionPoint and search for a
ConfigProperty
annotation or an Annotation with a ConfigProperty meta-Annotation. |
protected String getStringPropertyValue(javax.enterprise.inject.spi.InjectionPoint injectionPoint)
Inspects the given InjectionPoint and search for a ConfigProperty
annotation or an Annotation with a ConfigProperty
meta-Annotation.
The name and defaultValue information will be used to resolve the
configured value.
injectionPoint
- current injection pointprotected String getPropertyValue(String propertyName, String defaultValue)
propertyName
- the name of the property keydefaultValue
- the default value to return if no configured property is found or
ConfigProperty.NULL
if no default value should be returned.protected <T extends java.lang.annotation.Annotation> T getAnnotation(javax.enterprise.inject.spi.InjectionPoint injectionPoint, Class<T> targetType)
T
- typeinjectionPoint
- current injection pointtargetType
- target typeCopyright © 2014 The Apache Software Foundation. All rights reserved.