Custom annotations in SpringBoot

Posted by lanmonkey on Mon, 08 Nov 2021 19:41:51 +0100

annotation

1. Meta annotation

Meta annotations are annotations that define annotations. They are the basic annotations provided by Java to define annotations

annotationexplain
@RetentionIt is an annotation Class, implementing declaration Class, declaration Class and declaration extension
@TargetIt is placed on the top of the custom annotation to indicate the scope that the annotation can be used
@InheritedSubclasses are allowed to inherit the annotations of the parent class. Annotations using the parent class can be obtained in subclasses
@DocumentedIndicates that this annotation is recorded by javadoc
@interfaceThe type used to customize the annotation

1.1 @Target

The function of this annotation is to tell java where to put custom annotations, such as classes, methods, constructors, variables, etc

Its value is an enumeration type with the following property values

  • ElementType.CONSTRUCTOR: used to describe the constructor
  • ElementType.FIELD: used to describe member variables, objects and properties
  • ElementType.LOCAL_VARIABLE: used to describe local variables
  • ElementType.METHOD: used to describe the method
  • ElementType.PACKAGE: used to describe the package
  • ElementType.PARAMETER: used to describe parameters
  • ElementType.TYPE: used to describe a class, interface, or enum enumeration declaration

1.2 @Retention

This annotation is used to describe the life cycle of a custom annotation. There are three life cycles in the annotation

  • RetentionPolicy.RUNTIME: the annotation will never be discarded, and the annotation will be retained during the runtime. The information of the annotation can be read using the reflection mechanism
  • RetentionPolicy.CLASS: discarded when the class is loaded. This method is used by default
  • RetentionPolicy.SOURCE: discarded at the compilation stage. Custom annotations have no meaning after compilation, so they will not be written into bytecode. It's familiar

@Override belongs to this category

1.3 @Inherited

This annotation is a tag annotation indicating that the marked type can be inherited. If an annotation using this tag is used, the subclass of this class will also use this annotation

1.4 @Documented

This annotation indicates whether to add annotation information to the java document

1.5 @interface

This annotation is used to declare an annotation. Each method actually declares a configuration parameter. The method name is the parameter name, and the return value type is the parameter type

2. Implement a custom annotation

First create a custom annotation

package com.example.demo;


import org.springframework.stereotype.Component;

import java.lang.annotation.*;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MyAnnotation {
    String value();
}

Then write a business logic, which is the method to intercept the customized annotation, and then print the annotation parameters on the console

package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class TestAnnotationAspect {
    @Pointcut("@annotation(com.example.demo.MyAnnotation)")
    public void myAnnotationPointCut(){}

    @Before("myAnnotationPointCut()")
    public void before(JoinPoint joinPoint)throws Throwable{
        MethodSignature sign=(MethodSignature) joinPoint.getSignature();
        Method method=sign.getMethod();
        MyAnnotation annotation=method.getAnnotation(MyAnnotation.class);
        System.out.println("Test Annotation Parameters:"+annotation.value());
    }
}

Finally, a Controller is implemented to check whether the business logic is printed when accessing the page

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestAnnotationController {
    @GetMapping("/test")
    @MyAnnotation("Test custom annotations")
    public String test(){
        return "shelgi";
    }
}

The operation results are as follows

Topics: Java Spring Spring Boot