-
Notifications
You must be signed in to change notification settings - Fork 28
Custom Annotations
With Easy Properties, you can write your own annotations to load configuration properties from a custom location. The following steps describe how to create your own annotation and how to use it with Easy Properties.
First, you need to create your annotation with all the necessary information about where to load properties, which property to load, default values, etc. You can see examples of how built-in annotations are defined in the io.github.benas.easyproperties.annotations
package.
The AnnotationProcessor
interface allows you to specify how to process your annotation to load all the necessary parameters specified by the user.
The following is the definition of this interface:
public interface AnnotationProcessor<T extends Annotation> {
/**
* Process an annotation of type T.
*
* @param annotation the annotation to process.
* @param field the target field
* @return Return the object to set in the annotated field or null if the value should be ignored
* @throws AnnotationProcessingException thrown if an exception occurs during annotation processing
*/
Object processAnnotation(final T annotation, final Field field) throws AnnotationProcessingException;
}
You can see some examples of how the built-in annotations are processed in the io.github.benas.easyproperties.processors
package.
To register your custom annotation processor within Easy Properties, you can use the PropertiesInjectorBuilder
API as follows:
//Instantiate a properties injector and register custom annotation processor
PropertiesInjector propertiesInjector = new PropertiesInjectorBuilder()
.registerAnnotationProcessor(MyAnnotation.class, myAnnotationProcessor)
.build();
Now you can annotate any field with your annotation and Easy Properties will automatically use the registered annotation processor to load the value, convert it to the field type and set it to the field value.
Easy Properties is created by Mahmoud Ben Hassine and the awesome contributors
-
Introduction
-
Documentation