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 @interface MyCustomAnnotation {
String value() default ""; // A method with a String return type
int number() default 0; // A method with an int return type
boolean enabled() default true; // A method with a boolean return type
Class<?> type() default Void.class; // A method with a Class return type
Class<? extends SomeFactory> someFactory(); // A method with a Class return type
MyEnum enumValue() default MyEnum.DEFAULT; // A method with an enum return type
String[] arrayValue() default {}; // A method with an array return type
}
enum MyEnum {
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.
public abstract class SomeFactory {
public abstract Event triggerEvent(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.
package org.example;
import org.example.reflection.Logger;
import org.example.service.SampleService;
public class Application {
public static void main(String[] args) {
var sampleService = new SampleService();
Logger.logMethodEntryExit(sampleService);
}
}
Execute the main program and verify the logs.
Case 2: With Class and Field level custom annotation
Create custom annotation's which can be applied to the class and fields.