Java Annotation Overview

  • 2020-04-01 03:00:15
  • OfStack

Overview of JAVA annotations:

1. Annotations are for the compiler, which is different from annotations

2. Three basic notes:

@ Override  Tells the compiler that this is overwriting the method
@deprecated tells the compiler that the method is out of date
@ SuppressWarnings (" unchecked ")   Don't warn
= (value = {} "unchecked")
3. Annotations can be used as an alternative to traditional configuration files
Starting with JDK5, Java added support for MetaData, or annotations.

Custom annotations and reflection annotations

Custom comments:
1. New annotation :(only an @ sign more than the definition of the interface)


public @interface myAnnotation {  
    //Property  
    String who();  
    int age();  
    String gender();  
}  

2. Set annotations with default values


public @interface YouAnnotation {  
    String who() default "tom";  
    int age() default 0;  
    String gender() default "female";  
}  

3. Array case


public @interface TheyAnnotation {  
    String[] value(); //There must be ()& cake;
}  

Yuan Annotation/MetaAnnotation
It's used to modify annotations. (see the @override source code)

The @retention Annotation policy, which specifies the fields that the Annotation can retain
RetentionPolicy. CLASS
At the bytecode level, not visible at the run level (default)
RetentionPolicy. The RUNTIME
All three levels are visible and can be reflected at runtime
RetentionPolicy. SOURCE  Available only at the source level, not at the bytecode level

@ Target  Specifies the scope in which annotations can be used
@documented writes to a document, and when an HTML document is written using the javadoc command, the annotation is written along with it
@ Inherited  Inheritable. Subclasses that inherit this class still have the property of the parent class annotation

Ex. The way to connect to the database by reflecting annotations:

Define comments as follows:


//Allows an annotation to be reflected at run time & NBSP;
@Retention(RetentionPolicy.RUNTIME)  
public @interface DbInfo {  
    String driver() default "com.mysql.jdbc.Driver";  
    String url() default "url = jdbc:mysql://localhost:3306/academic";  
    String password() default "1234";  
    String username() default "root";  
}  

Reflection comments:


@DbInfo  
public static Connection getConnection() throws Exception{  
    //Gets the bytecode & NBSP; of the class;
    Class clazz = Demo2.class;  
    //Gets a public method named getConnection() in this class & NBSP;
    //Parameter 1: method name & NBSP;
    //Parameter 2: the bytecode object corresponding to the method type parameter, null  if not, null 
    Method method = clazz.getMethod("getConnection", null);  
    //Through this method, gets the annotation & PI defined on the method;
    DbInfo dbInfo = method.getAnnotation(DbInfo.class);  
    String driver = dbInfo.driver();  
    String url = dbInfo.url();  
    String user = dbInfo.username();  
    String password = dbInfo.password();  

    Class.forName(driver);  
    return DriverManager.getConnection(url, user, password);  
}  


Related articles: