Scenarios for annotation :

Yes web We all know about it , Many development frameworks use annotations , such as Spring The code is as follows :
@RequestMapping(value="/inputPerson") public String inputPerson(){
System.out.println("..........inputPerson"); return "inputPerson"; }
@RequestMapping(value="/updatePerson") public String updatePerson(){
System.out.println("..........updatePerson"); return "updatePerson"; }
In this code  RequestMapping It's just a note , Note with @ Symbolic modification , This annotation applies to methods , Inside value For annotation assignment .

annotation type :

1: Standard notes :

Override effect : Guaranteed compile time Override The correctness of function declaration .

Deprecated effect : Guaranteed compile time Override The correctness of function declaration .

SuppressWarnings Turn off specific warnings , His main parameters are :

1):deprecation: Warning when obsolete classes or methods are used

2):unchecked   Warning when an uncalibrated conversion is performed

3):path   In classpath , There is a warning that there is a non-existent path such as the source file path

4):serial When a class in a sequenceable speech is missing

5):serialVersionUID Warning when defining

6):finally whatever finally Warning when clause cannot complete properly

7):all Warning on all of the above

 

Meta annotation :( Responsible for annotation and other notes )

@Retention  ( main parameter :SOURCE CLASS  RUNTIME)

@Target
   ( main parameter :CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE)

@Documented (jiang Include comments in Javadoc in )

@Inhertied( Allow subclasses to inherit comments from parent classes )

 

Look at the following example :

We define an annotation , This annotation applies to all fields Target Of value The value is ElementType.FIELD
import java.lang.annotation.ElementType; import
java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) public @interface FiledAnnotation { String
userName() default "zhangming"; String passWord() default "123456"; String
unit() default "android"; }
definition Person Using annotations , And by annotating to Person Object's field assignment :
public class Person { @FiledAnnotation(userName=" Xiao Ming ") public String userName;
@FiledAnnotation(passWord="abc123456") public String passWord;
@FiledAnnotation(unit="java") public String unit; 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; } public String
getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit;
} @Override public String toString() { return "Person [userName=" + userName +
", passWord=" + passWord + ", unit=" + unit + "]"; } }
Test results to see if the annotation works correctly :
public static void main(String[] args) { // Create a Person object Person person = new
Person(); // obtain person The field array corresponding to the object Field[] fields = person.getClass().getFields();
for(int i = 0;i < fields.length;i++){
// Judge whether it is currently userName field , also userName Is the annotation in the field yes FiledAnnotation
if(fields[i].isAnnotationPresent(FiledAnnotation.class) &&
fields[i].getName().equals("userName")){ FiledAnnotation filedAnnotation =
fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.userName());
// Judge whether it is currently passWord field , also userName Is the annotation in the field yes FiledAnnotation }else
if(fields[i].isAnnotationPresent(FiledAnnotation.class) &&
fields[i].getName().equals("passWord")){ FiledAnnotation filedAnnotation =
fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.passWord());
// Judge whether it is currently unit field , also userName Is the annotation in the field yes FiledAnnotation }else
if(fields[i].isAnnotationPresent(FiledAnnotation.class) &&
fields[i].getName().equals("unit")){ FiledAnnotation filedAnnotation =
fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.unit()); } } }
Through the above example, the console print results are as follows :

Xiao Ming

abc123456

java

Here we are Person Object to assign the current object field with annotation , Get the field name of the current object by reflection method , Then call isAnnotationPresent Determine whether the current field contains FiledAnnotation annotation , If it is , Finally, it was approved getAnnotation() Method FiledAnnotation example , Last output current person The value assigned by the object annotation .

 

 

 

 

 

 

 

 

 

Technology
©2020 ioDraw All rights reserved
error: (-215:Assertion failed) Solution python Simple record of network programming mac solve Enter passphrase for key Enter the password each time Huawei Hongmeng system learning notes 9- Ecological construction of developers Apple iPhone 12 Price leakage : The official highest price is close to 1 Ten thousand yuan Docker Import of containers and mirrors , export Cross analysis of data analysis Notes on core principles of reverse engineering ( One )——Hello World-1Python Student information management system ( Lite ) Ant old employees leave early and miss millions of wealth , Some people say they don't regret it