preface

We all use annotations in Java, especially for framework learning. So what’s the appeal of annotations? How does it work in the program? How to understand the existence of annotations! Relevant information will tell us that annotations are to explain the program, explain the program! So what does it say about the program? And the parameters in the annotations, have you ever wondered where they go? The following is a demonstration.

annotations

Annotations, also called metadata. A code level specification. It is a feature introduced in JDK1.5 and later and is on the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., used to describe and comment these elements.

reflection

  • JAVA reflection is in the running state, get the structure of any class, create an object, get a method, execute a method, property!
  • Reflection: The soul of frame design
  • Encapsulating the components of a class into other objects is the reflection mechanism

Reflection gives life to the note:

The following illustrates the value of annotations and where the annotation parameters go by using custom annotations (simulating the mapping in the database) :

  1. Define annotations (database annotations TableAnnotation, field annotations ColumnAnnotation)

TableAnnotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnotation {
    String value(a);// When a method is annotated, the default name is value
}
Copy the code

ColumnAnnotation

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnAnnotation {
    /** * column name *@return* /
    String columnName(a);

    /** * Field type *@return* /
    String type(a);

    /** * Field length *@return* /
    String length(a);
}
Copy the code
  1. Define the Java class Book to use annotations to declare information such as database names and field properties

Book.java

@TableAnnotation("test_Book")// use value as the name
public class Book{
    @ColumnAnnotation(columnName = "id",type = "int",length = "10")
    private int id;
    @ColumnAnnotation(columnName = "name",type = "varchar",length = "20")
    private String name;
    @ColumnAnnotation(columnName = "info",type = "varchar",length = "25")
    private String info;

    public Book(a) {}

    public int getId(a) {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo(a) {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null|| getClass() ! = o.getClass())return false;
        Book book = (Book) o;
        return id == book.id &&
                Objects.equals(name, book.name) &&
                Objects.equals(info, book.info);
    }
    @Override
    public int hashCode(a) {
        return Objects.hash(id, name, info);
    }
    @Override
    public String toString(a) {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\' ' +
                ", info='" + info + '\' ' +
                '} '; }}Copy the code
  1. The life of annotations is to use Java’s reflection mechanism to retrieve annotation information for use purposes! Create a test class to take advantage of reflection and get the annotation life of the Book class!
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException {
        // Load the Class and get the Class object
        Class bk = Class.forName("com.kkb.Demo3.Book");
        // Get the constructor
        TableAnnotation annotation = (TableAnnotation) bk.getAnnotation(TableAnnotation.class);
        // Get the annotation information for the class annotation
        String value = annotation.value();
        System.out.println(value);
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        
        // Get the annotation information for the attribute annotation
        Field[] fields = bk.getDeclaredFields();
        for (Field f:fields) {
            ColumnAnnotation ca = f.getAnnotation(ColumnAnnotation.class);
            System.out.println(f.getName()+"Property, corresponding field:"+ca.columnName()+", the data type is"+ca.type()+", the length is"+ca.length()); }}}Copy the code

Running results:

conclusion

Annotation is to use reflection, get the information and parameters of the annotation, and then get the parameter value or annotation type, to do some operations, so that the annotation and annotation parameters and other information has its use value!