How to create Custom Annotation in Java Spring Boot

How to create Custom Annotation in Java Spring Boot

Hello everyone, In this article we are going to talk about Annotations in Java and we will we are going to create our own annotation to improve code in Spring boot.

Let's get started

What is the annotation in Java?

Annotations provide metadatas for the code blocks. With annotation some additional informations will be passed to compiler and these informations are being used for compiling for sometimes they are working also at runtime and we can perform some operations.

If we can give an example for usages, we can define an annotation to limit the access to a method according to user authentication level. We can perform some of the pre-checks by the help of annotations. As we know springboot is using annotations mainly to reduce complexity and improve the code quailty and maintainability.

We put annotations before or above line of decleration and annotations starts with @. Below you can see an the syntax:

public void methodName(...){
Below you can see some of the annotations in spring boot :
  • @Service annotation specifys that is the related class is an service class and business logic will be defined within.
  • @Repository annotate the object will be used to access to database directly and manage database operations.
  • @Controller annotation specify the class contains the endpoints with through the url configured with @RequestMapping annotation
  • @SpringBootApplication specifies that a class is initializing a spring boot application, it initialize enablin configuration and component scanning for spring boot application initialization.

Now lets create our own annotation in Java SpringBoot

Firstly we need to create an interface for for the annotation BUT this is not a regulare interface so we will need to define it with @interface keyword. With this decleration also we will need to specify our annotation configurations like target element type or where it will be considered.

Below you can see the decleration of an annotation interface.

package com.thecodeprogram.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public @interface CheckLevel {
	int level() default 0;

In above example as you can see we have created a an annotation which named as CheckLevel and it is taking parameter as level. Also we defined default value for level is 0. Also we defined target as element is methods and it will be there during the program Runtime also.

So now we need to create the logic for this annotation to define what it will do.

package com.thecodeprogram.annotation;

import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

public class CheckLevelLogic {
	ProceedingJoinPoint proceedingJoinPoint;
	public Object validateAspect(ProceedingJoinPoint pjp) throws Throwable {
		this.proceedingJoinPoint = pjp;
		MethodSignature signature = (MethodSignature) this.proceedingJoinPoint.getSignature();
		Method method = signature.getMethod();

		PermissionLevel validateAction = method.getAnnotation(CheckLevel.class);
		int level = validateAction.level();

		System.out.println("Level is: " + level);
So now lets see what we have done one by one in here
  • Firstly we have created a class for logic implementation of our annotation and we used Aspect and Component annotations for this class.
  • Then we implementad aour validateAspect method for our annotation, with Around annotation we specified that this method is for our custom annotation.
  • Then we Used ProceedingJoinPoint and Medhat signature to access related method and we will call our check function before related method invoked.
Now lets see how our custom annotation is being used below:

package com.thecodeprogram.controller;

import com.thecodeprogram.annotation.CheckLevel;

public class UserController {
	@CheckLevel(level = 2)
	public void deleteData(@PathVariable String id) {
		// ......
		// Logic implementation
		// .....
As you can see above we just used annotation with a variable. and this annotation will be invoked before method invoked.

That is all

Burak Hamdi TUFAN


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!


  • There is no comment. Be the owner of first comment...