编程知识 cdmana.com

The use of Java annotations

java The use of annotations in

1. Let's go back to the front

The benefits of annotation development

1. Make the code cleaner and easier to read , Easy to maintain and modify . such as , I used to use spring Development of , It's all based on xml The file realizes unified configuration management , But the disadvantages are also obvious , As the project gets bigger and bigger ,xml The files will get more and more complicated , Maintenance costs will also be higher and higher . Using annotations can provide greater convenience , Easy to maintain and modify .

2 Can implement code type checking , Especially in the compiler's point of view to achieve some type checking , For example, pre inspection (@Override) And suppress warnings (@SuppressWarnings) etc. .

3 Custom annotation , As a carrier of additional information , Store additional information about the program

2 Classification and use of annotations

​ Java Annotations are bits of meta-information attached to code , For compiling and runtime parsing and using , Play a show 、 Configured functionality .

Annotations don't affect the actual logic of the code , Play a supporting role only . Included in java.lang.annotation In bag . The definition of annotations is similar to the definition of interfaces , Use @interface To define , Defining a method defines an element for the annotation type , Method declaration does not allow parameters or throw sentence , The return value type is limited to the original data type 、 character string String、Class、enums、 Annotation type , Or the array of the previous ones , Methods can have default values . Annotations don't directly affect the semantics of the code , But it can be seen as a program tool or class library . It, in turn, has an impact on the semantics of the running program . Annotations can be from source files 、class The file can be read by reflection mechanism at runtime .

Generally speaking , There are three types of annotations : Yuan notes , Standard notes , Custom annotation

2.1 Yuan notes

Meta annotation is responsible for annotating other annotations , It mainly indicates the scope of use of the annotation , Effective range . We can't change it , It can only be used to define our own annotations .

Include @Retention @Target @Document @Inherited Four kinds of .(java.lang.annotation Provided in the , For annotation type ).

annotation explain
@Target Define the purpose of the annotation , In other words, annotations can be defined to act on classes , On the way , Or variables
@Retention Define retention policies for annotations .RetentionPolicy.SOURCE: Annotations only exist in source code , stay class The bytecode file does not contain ;RetentionPolicy.CLASS: Default retention policy , Annotations will be in class Bytecode file exists , But the runtime can't get ;RetentionPolicy.RUNTIME: Annotations will be in class Bytecode file exists , At run time, you can get... By reflection .
@Document Indicate that the annotation will be included in javadoc in
@Inherited Indicates that the subclass can inherit the annotation in the parent class

Target Type mainly depends on ElementType This type , The specific types are as follows :

Target type explain
ElementType.TYPE Interface 、 class 、 enumeration 、 annotation
ElementType.FIELD Field 、 Enumerated constant
ElementType.METHOD Method
ElementType.PARAMETER Method parameter
ElementType.CONSTRUCTOR Constructors
ElementType.LOCAL_VARIABLE local variable
ElementType.ANNOTATION_TYPE annotation
ElementType.PACKAGE package

2.2 Standard notes

Java The standard notes provide three , It's defined in java.lang The annotations in , I think these three annotations are more of a kind of annotation

  • @Override Indicates that the method in the parent class is overridden by the current method .

  • @Deprecated Mark an element as expired , Avoid using

The supported element types are :CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE

  • @SuppressWarnings The compiled output does not correspond to

    A simple use demo:

    @SuppressWarnings(value = {"unused", "rawtypes"})
    public class Children  extends Parent{
        @Override
        public void work() {
            System.out.println(" I'm a rewritten method ");
        }
    
        @Deprecated
        public void play(){
            System.out.println(" This method is not recommended ");
        }
    }
    

2.3 Custom annotations implement a sql Statement splicing

What needs attention : The definition of annotations is similar to the definition of interfaces , Use @interface To define , Defining a method defines an element for the annotation type , Method declaration does not allow parameters or throw sentence , The return value type is limited to the original data type 、 character string String、Class、enums、 Annotation type , Or the array of the previous ones , Methods can have default values .

Custom annotations can be divided into three steps : Definition notes , Using annotations , Read annotation

Definition notes

@Target(ElementType.TYPE) // Annotation loading class 
@Retention(RetentionPolicy.RUNTIME) //  Read annotations at run time 

public @interface Table {
    String value(); 
}


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public@interface UserFiled {
    String name();
    String type();
    int length();
}

Using annotations

//  Add custom annotations to the user , Implement a table mapping 
@Table(value = "user_table")
public class User {

    @UserFiled(name = "user_id",type = "int",length = 8)
    private int userId;

    @UserFiled(name = "user_name",type = "varchar",length = 16)
    private String userName;

    @UserFiled(name = "password",type = "varchar",length = 16)
    private String password;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Read the content of the annotation

/**
 *  Read the value in the annotation 
 */
public class GetUser {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class userClass = Class.forName("annocation.blog.User");

        //  Read the annotations on the class 
        Table table = (Table) userClass.getAnnotation(Table.class);
        System.out.println(table.value());

        //  Read comments on attributes 
        Field userId = userClass.getDeclaredField("userId");
        UserFiled userFiledId = userId.getAnnotation(UserFiled.class);
        System.out.println(userFiledId.length() + "----" + userFiledId.type() + "-----" + userFiledId.name());

        Field userName = userClass.getDeclaredField("userName");
        UserFiled userFiledName = userName.getAnnotation(UserFiled.class);
        System.out.println(userFiledName.length()+"----"+userFiledName.type()+"----"+userFiledName.name());

        Field password = userClass.getDeclaredField("password");
        UserFiled userFiledPassword = password.getAnnotation(UserFiled.class);
        System.out.println(userFiledPassword.name() + "-----" + userFiledPassword.type() + "-----" + userFiledPassword.length());

        //  Splice one sql sentence 
        String name = "chenwei";
        String sql ="select * from" + table.value()+"where"+userFiledName.name()+"="+name;
    }
}

result :

user_table
user_id----int-----8
user_name----varchar----16
password-----varchar-----16

The implementation process of custom annotation :

1, Definition notes

2, Using annotations , Use your own defined annotations to achieve some purpose , In this case , Annotation is used to complete the mapping relationship between database table and entity class

3 Read the content of the annotation , It's also an important part , The core still uses the idea of reflection , Get the class that uses annotations , According to the class getAnnotion Method to get the definition of annotation , Get the value on the annotation .

3 The principle of annotation implementation

A large part of the principle of annotation implementation is based on reflection implementation .

​ Reflection can get Class object , And then get Constructor、Field、Method Etc , Click open source structure to find Class、Constructor、Field、Method And so on AnnotatedElement Interface ,AnnotatedElement The interface method is as follows

//  Determines whether the element contains the specified annotation , Include returns true
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
    
//  Returns the corresponding annotation on the element , If there is no return null
<T extends Annotation> T getAnnotation(Class<T> annotationClass);

//  Returns all annotations on the element , If there are no annotations, an empty array is returned 
Annotation[] getAnnotations();

//  Returns the specified type of annotation , If it doesn't return an empty array 
T[] getAnnotationsByType(Class<T> annotationClass)
    
//  Returns the specified type of annotation , If it doesn't return an empty array , Only annotations with direct annotation , It doesn't contain inherited Annotations 
T getDeclaredAnnotation(Class<T> annotationClass)
    
//  Returns the specified type of annotation , If it doesn't return an empty array , Only annotations with direct annotation , It doesn't contain inherited Annotations 
T[] getDeclaredAnnotationsByType
    
//  Returns all annotations on the element , If there are no annotations, an empty array is returned , Only annotations with direct annotation , It doesn't contain inherited Annotations 
Annotation[] getDeclaredAnnotations();

Through an example to illustrate the use of annotations again :

Definition notes

@Documented
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotaion {
    String getValue() default "this is myAnntaion";
    int order() default 0;
}

Using annotations

@MyAnnotaion(getValue = "annotation on class")
public class Demo {

    @MyAnnotaion(getValue = "annotation on filed")
    public String name;

    @MyAnnotaion(getValue = "annotation on method")
    public void hello(){
    }

    @MyAnnotaion
    public void defaultMethod(){

    }
}

Using reflection to read values in annotations .

public class TestDemo {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        /**
         *  Gets the annotations on the class 
         */
        Class<Demo> demoClass = Demo.class;
        Annotation[] annotaion = demoClass.getAnnotations();
        printAnnotation(annotaion);

        /**
         *  Read comments on member variables 
         */
        Field name = demoClass.getField("name");
        Annotation[] getOnFiled = name.getAnnotations();
        printAnnotation(getOnFiled);

        /**
         *  Read the annotation on the method 
         */
        Method hello = demoClass.getMethod("hello", null);
        MyAnnotaion onMethod = hello.getAnnotation(MyAnnotaion.class);
        System.out.println(onMethod.getValue());

        /**
         *  Get annotations on the default method 
         */
        Method defaultMethod = demoClass.getMethod("defaultMethod", null);
        MyAnnotaion onDefaultMethod = defaultMethod.getAnnotation(MyAnnotaion.class);
        System.out.println(onDefaultMethod.getValue());

    }

    public static void printAnnotation(Annotation... annotations) {
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
    }
}

Running results

@annocation.MyAnnotaion(getValue=annotation on class, order=0)
@annocation.MyAnnotaion(getValue=annotation on filed, order=0)
annotation on method
this is myAnntaion

Reference material :

http://blog.kimzing.com/java/Java From introduction to mastery - It's enough to learn this one /

《java Programming idea 》

版权声明
本文为[chenweicool]所创,转载请带上原文链接,感谢

Scroll to Top