Custom Annotation
Overview about custom annotation.
About
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
@interfacekeyword. 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
}Target Elements: Specify where the annotation will be used by annotating the annotation declaration with
@Target. For example,ElementType.METHODspecifies that the annotation can be applied to methods.
Retention Policy: Specify the retention policy for the annotation using
@Retention. This determines how long the annotation's metadata is kept.RetentionPolicy.RUNTIMEmeans the annotation will be available at runtime via reflection.
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.
Examples
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
Reference
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.
Loggable.java
Create a sample service and apply the custom annotation.
SampleService.java
Create a class to process the annotation.
Logger.java
Create a Main application.
Application.java
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.
JsonSerializableField.java
JsonSerializableClass.java
Create sample Product class and apply the annotations.
Product.java
Create reflection class
JsonSerializable.java
Create main application class and execute the program

Last updated