Custom annotations allows us to add metadata to the code. This metadata can then be used by Spring Boot or other frameworks at different stages (compile time, runtime, or both) to enhance functionality and improve code clarity.
How to create and use custom annotations?
Declaration: Custom annotations are defined like any other Java interface, with the @interface keyword. Annotations can include elements that act as parameters.
Annotation Methods: Custom annotation methods, which is optional, cannot have parameters and cannot throw exceptions. They can only have return types like primitives, String, Class, enums, annotations, or arrays of these types.
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interfaceMyCustomAnnotation {Stringvalue()default""; // A method with a String return typeintnumber()default0; // A method with an int return typebooleanenabled()defaulttrue; // A method with a boolean return typeClass<?>type()defaultVoid.class; // A method with a Class return typeClass<? extends SomeFactory>someFactory(); // A method with a Class return typeMyEnumenumValue()defaultMyEnum.DEFAULT; // A method with an enum return typeString[] arrayValue()default {}; // A method with an array return type}enumMyEnum { DEFAULT, OPTION1, OPTION2}
// SomeFactory class use above can contain several implementation classes used in AOP.// For eg. we can have custom @Notification annotation which takes the implementation class such as SMSNotification, MailNotification etc.
publicabstractclassSomeFactory {publicabstractEventtriggerEvent(JoinPoint joinPoint,Object result);}
Target Elements: Specify where the annotation will be used by annotating the annotation declaration with @Target. For example, ElementType.METHOD specifies that the annotation can be applied to methods.
Element Types - Applicable Type
TYPE - class, interface or enumeration
FIELD - fields
METHOD - methods
CONSTRUCTOR - constructors
LOCAL_VARIABLE - local variables
ANNOTATION_TYPE - annotation type
PARAMETER - parameter
Retention Policy: Specify the retention policy for the annotation using @Retention. This determines how long the annotation's metadata is kept. RetentionPolicy.RUNTIME means the annotation will be available at runtime via reflection.
RetentionPolicy.SOURCE - Refers to the source code, discarded during compilation. It will not be available in the compiled class.
RetentionPolicy.CLASS - Refers to the .class file, available to java compiler but not to JVM. It is included in the class file.
RetentionPolicy.RUNTIME - Refers to the runtime, available to java compiler and JVM.
Use the Annotation: Once defined, custom annotation can be used throughout the Spring Boot application.
Processing Custom Annotations: Reflection or Spring AOP (Aspect-Oriented Programming) can be used to process custom annotations at runtime. Reflection allows to access the annotation information using libraries like java.lang.reflect. Spring AOP enables to create aspects that intercept method calls based on the presence of annotations like @LogExecutionTime, @SMSNotification.
Use case
Custom annotations are commonly used in Spring Boot for various purposes like request mapping, security, transaction management, logging, notification and more. They help in making the code more expressive, readable, and maintainable by providing metadata that can be leveraged by frameworks and developers.
Scenario 1: Using custom annotation in AOP
Scenario 2: Using Reflection
Case 1: With Method level custom annotation
Create a custom annotation which can be applied to the methods and available at runtime.