Definition of Java annotation

Keywords: jvm Attribute

Define annotation syntax

public @interface MyAnnotation {
    String className() default "";
    String methodName();
@Target annotation description

@Target is the location restriction for annotation, and the parameter is ElementType[] value();

Enumeration name describe
TYPE Used to describe a class, interface (including annotation types), or enum declaration
FIELD Used to describe a domain
METHOD Used to describe the method
PARAMETER Used to describe parameters
CONSTRUCTOR Used to describe the constructor
LOCAL_VARIABLE Used to describe local variables
PACKAGE Used to describe a package

@The retention annotation states that this type of annotation will be retained until that stage, RetentionPolicy value(); the annotation will be retained by the virtual machine so that it can be read by reflection at runtime.

Enumeration name describe
SOURCE Annotations of this type are reserved only at the source level and are ignored at compile time
CLASS Annotations of this type are preserved at compile time and exist in the class file, but will be ignored by the JVM
RUNTIME Annotations of this type will be preserved by the JVM, so they can be read and used by the JVM or other code using reflection mechanism at runtime

The Documented annotation indicates that this annotation should be recorded by the javadoc tool. By default, javadoc does not include annotations. However, if @ Documented is specified when the annotation is declared, it will be processed by tools such as javadoc, so the annotation type information will also be included in the generated document.

Annotation attribute definition

  • The properties of annotations are also called member variables. Annotation has only member variables, no methods.
  • The member variables of annotation are declared in the form of "method without parameter" in the definition of annotation
  • Its method name defines the name of the member variable, and its return value defines the type of the member variable.
  • The attribute in the annotation can have a default value, which needs to be specified with the default key value.
  • value can be used without specifying a name

Easy to use

1. Define a note
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})//At the same time, it can act on class, method and attribute.
@Retention(RetentionPolicy.RUNTIME)//When the annotation was scanned
@Documented//Document annotation class
public @interface MyAnnotation {
    String value()default "";
     * Operation module
     * @return
    public String module() default "";

     * Operation content: for example: Add User
     * @return
    public String operationDesc() default "";
2. Use notes
 @MyAnnotation(module = "get", operationDesc = "Test custom comments")
 public String get() {
     return "GET Hello World";
3. Define section use
public class SystemLogAspect {
    private static final Logger log = LoggerFactory.getLogger(SystemLogAspect.class);

    @Pointcut("execution(* com.swagger.demo.controller..*(..))&&@annotation(com.swagger.demo.annotation.MyAnnotation)")
    public void controllerAspect() {


    public Object aroundMethod(ProceedingJoinPoint point) {
        String methodTarget = point.getTarget().getClass().getName() + "." + point.getSignature().getName() + "()";
        MethodSignature signature = (MethodSignature) point.getSignature();
        String methodName = signature.getName();
        Method method = signature.getMethod();
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        String module = annotation.module();
        String desc = annotation.operationDesc();"Operation module of advance notice: " + module + ",Operation method: " + methodTarget + ",Operational behavior: " + desc);
        Object result = null;

        try {
            result = point.proceed();
        } catch (Throwable throwable) {
        } finally {
  "Operation module of post notice: " + module + ",Operation method: " + methodTarget + ",Operation behavior: " + desc+",Return results: "+result);
        return result;


Posted by girlzz on Sat, 27 Jun 2020 22:26:18 -0700