Talk about annotations and reflections most beginners are prone to face-jerk, which is why? I think there are two main reasons: 1, a lot of people work in the development of very little use, no practice is just to look at the words, 80% people will forget, how to annotate the same as the set frame everyone will use every day, maybe we will not think this thing has what can be mysterious 2, Annotation and reflection of the two parts of the knowledge on the network are not much also be transmitted very damndest appearance, compared with less systematic resources. So a lot of people are conservative, this article will be from the following aspects of the annotation related knowledge, does not contain meta-annotations related knowledge, only to explain the custom annotations. , if you're a really passionate person, I hope you'll be able to see it through.
1. Definition of annotations (what exactly is a note?) )
2. Code interpretation (write a note with your own hand)
3. Extraction of annotations
1, the definition of annotations:
What exactly is an annotation? First look at the definition of Baidu Encyclopedia:
Annotations (Annotation), also called Meta data. A description of the code level. It is a feature introduced in JDK1.5 and later versions, with classes, interfaces, and enumerations at the same level. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on, to explain these elements, comments.
To be honest, the definition is accurate, but it's too abstract, but you can find some key statements such as "code-level descriptions," "annotations," and so on. This means that the annotation actually works the same as our usual "/** */" comment: It will not have any effect on our code, if you do not deal with it. Give a more illustrative example: We are one person, but we may have multiple occupations, such as teachers, workers, farmers, students. The same person may be both a farmer and a teacher. Here the "people" can be seen as our code, teachers, workers, farmers, students and a series of occupations is the annotation, like a label affixed to our people, of course, you can also no label is a common person, even if you have a label, you are a peasant teacher, if you do not say and nobody knows, It doesn't make any difference to you, you know?
2, the code implementation of the annotations: about the definition of annotations and all the properties of the direct look at the following code OK
1 PackageCom.daihui.quartz;2 3 ImportJava.lang.annotation.ElementType;4 Importjava.lang.annotation.Retention;5 ImportJava.lang.annotation.RetentionPolicy;6 ImportJava.lang.annotation.Target;7 8 @Retention (retentionpolicy.runtime)9 /**Ten *retention: The retention period, which describes the life cycle of our annotations. One * Retentionpolicy commonly used values: A * Source annotations are retained only in the source phase, and are discarded when compiled by the compiler. - * CLASS annotations are only persisted to the time of compilation and will not be loaded into the JVM. - * Runtime annotations can be retained when the program is running, it will be loaded into the JVM, so they can be obtained when the program is running. the */ - @Target (Elementtype.annotation_type) - /** - * Target: The scope of the annotation. Define where our annotations can be used. + * ElementType commonly used values: - * Type: You can annotate a type, such as class, interface, enumeration + * FIELD: Tag attribute A * Method: Marking Methods at * PARAMETER: Tag method Internal parameters - * CONSTRUCTOR: Tag construction method - * local_variable: Tag Local variables - * Annotation_type: Annotations can be annotated to another annotation, i.e. nested annotations - * Package: Tag Pack - */ in //annotation definitions and interfaces are similar but need to be decorated with the @ symbol - Public@InterfaceTeacher { to //Next we can define the annotation's properties, i.e. the annotation's member variable + //annotations have only member variables, no methods. The member variables of the annotations are declared in the definition of the annotations in the form of an "invisible parameter method", - //Its method name defines the name of the member variable, and its return value defines the type of the member variable. as follows: the String ID (); * $ /**Panax Notoginseng * The default value of the property needs to be specified with the Defaults keyword and the type must be - * 8 basic data types plus classes, interfaces, annotations, and their arrays. the */ +String Profession ()defaultLanguage; A}
3, since we have customized the annotations, of course, to use and extract the information of our defined annotations, here we need to use the reflection-related knowledge to extract the annotation information:
PackageCom.daihui.quartz;ImportJava.lang.annotation.ElementType;ImportJava.lang.annotation.Target;/*** Package-name:com.daihui.quartz * Project-name:quartz * Function: *@authorDaihui *@since2018/3/31 0031 23:13*///Here we use our custom annotations tag we are a teacher and a teacher who teaches English.@Teacher (type = 1,profession = "English") Public classPerson { Public Static voidMain (string[] args) {/*** Since the annotations we defined above are @Target (Elementtype.type), that is, it acts on the class * so we use class here to get the information about the annotations*/ //isannotationpresent (class<? extends Annotation> Annotationclass) method to determine whether an annotation is applied to our person class BooleanIsannotationpresent = person.class. isannotationpresent (Teacher.class); System.out.println ("Isannotationpresent:" +isannotationpresent); /*** Getannotation (class<a> annotationclass) method directly gets the annotation of the specified type * or uses getannotations () to get directly the number of annotations applied on the class Group*/Teacher Annotation= person.class. getannotation (Teacher.class); System.out.println ("Type:" +Annotation.type ()); System.out.println ("Profession:" +annotation.profession ()); The result of the operation is as follows: Isannotationpresent:trueType:1Profession: English
Annotations that act on a method or field can be obtained, but not by class, but by field or method, which is not mentioned here, I hope you can practice the impression
See here, maybe we still don't know what the annotations are for? If you see this, then move on to the code above to get the annotation information, and think about it: We got this guy's message and know he's a teacher, if you have a child, is it possible that the teacher is your child's teacher, then you can talk about life with the teacher?
Summarize:
- If the annotations are difficult to understand, you can label them like tags in order to interpret things, annotations in order to interpret the code.
- Annotations are created as interfaces, but with an @ sign.
- The attributes of the annotations contain 8 basic types, as well as classes, interfaces, annotations, and their arrays
- Annotation extraction requires the help of Java-based reflection technology, and because of the slow reflection, the use of annotations also requires careful calculation of time costs.
A piece of article to make you understand Java Annotation