Six, custom annotations

1. The grammar of custom annotations

When using @interface to define custom annotations, it automatically inherits the java.lang.annotation.Annotation interface

1) @interface is used to declare an annotation

2) Each of these methods actually declares a configuration parameter

a) The name of the method is the name of the parameter

b) The return value type is the parameter type (the return value type can only be the basic type, Class, String, enum)

c) The default value of the parameter can be declared by default d) If there is only one member, the general parameter name is value

Precautions:

  • Annotation elements must have values. When we define annotation elements, we often use empty strings and 0 as the default.
  • Also often use negative numbers (such as -1) to indicate the meaning of non-existence

2. Meta annotations

        The function of meta-annotation is to be responsible for annotating other annotations. Four standard meta-annotation types are defined in Java, and they are used to provide explanations for other annotation types

        These types and the classes they support can be found in the java.lang.annotation package

1) @Target 2) @Retention 3) @Documented 4) @Inherited

Three, @Target

Role: Used to describe the scope of use of annotations (that is, where the described annotations can be used)

 举例:@Target(value=ElementType.TYPE)

Four, @Retention 

Function: indicates at what level the annotation information needs to be saved, and is used to describe the life cycle of the annotation

 Custom note 1:

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

/**
 * ClassName:MyAnnotation
 * date: 2020/4/16 10:25
 *
 * @author 王鼎禹
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotation {
    String stuName() default "";  //方法的名称就是参数的名称stuName()
                                  //The return value type is the parameter type (the return value type can only be the basic type, Class, String, enum)
                                   // default to declare the default value of the parameter, often use an empty string, 0 as the default value 
    int age () default 0 ; 
    String [] school () default {"B Station University", "Tsinghua University" }; 

}

Custom note 2:

 1 import java.lang.annotation.ElementType;
 2 import java.lang.annotation.Retention;
 3 import java.lang.annotation.RetentionPolicy;
 4 import java.lang.annotation.Target;
 5 
 6 /**
 7  * ClassName:MyAnnotation2
 8  * date: 2020/4/16 10:31
 9  *
10  * @author 王鼎禹
11  */
12 @Target({ElementType.TYPE,ElementType.METHOD})
13 @Retention(RetentionPolicy.RUNTIME)
14 public @interface MyAnnotation2 {
15      String value (); // If there is only one member, the general parameter name is value 
16 }
1  // @MyAnnotation This annotation can only be applied to methods
 2  // @ MyAnnotation2 (value = "aaaa") 
3 @ MyAnnotation2 ("aaa" )
 4  public  class Test5 {
 5      @MyAnnotation (stuName = "王 一一" )
 6      public  static  void show () {
 7      }
 8      @MyAnnotation (stuName = "乔 二 二", age = 23, school = {"Home Squatting University", "Lanxiang University" })
 9      public  void method () {
 10  
11      }
 12 }

 

Guess you like

Origin www.cnblogs.com/qiaoxin11/p/12711200.html