Java learning notes-annotations, java learning notes annotations
Annotations are undoubtedly a very interesting feature. I did not pay too much attention when I was reading a book. I recently saw custom annotations when I was reading Spring practices, so I couldn't help taking out the Java programming idea and re-reading the annotation chapter.
The form of annotation is written in the Code, such
@Override
Such code
Compared with annotations, the most important and interesting thing is that annotations can be customized, and reflection can also be used to call annotation classes and methods.
The format for creating custom annotations in Java is as follows:
/** file:Test.java*/package javaapplication;import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Test { public int id(); public String text() default "Null";}
So we can use annotations in the Code, such
/** File: JavaApplication. java */package javaapplication; import java. lang. reflect. method; class TestObject {@ Test (id = 233, text = "Hello") public void testA (int id, String text) {System. out. println ("id:" + id + "\ ttext:" + text) ;}@ Test (id = 10) public void testB (int id, String text) {System. out. println ("id:" + id + "\ ttext:" + text) ;}} public class JavaApplication {public static void main (String [] args) throws Exception {TestObject to = new TestObject (); // uses reflection to dynamically call the Method for (Method m: TestObject. class. getDeclaredMethods () {Test t = m. getAnnotation (Test. class); if (t! = Null) {m. invoke (to, t. id (), t. text ());}}}}
I briefly talked about the annotation writing method and then talked about the Metadata Annotation.
The meta annotation is used to annotate the annotation.
It is @ Target and so on. The following table is provided in specific Java programming ideology.
| @ Target |
Indicates where the annotation can be used. Possible ElementType parameters include:
- CONSTRUCTOR: CONSTRUCTOR Declaration
- FIELD: FIELD Declaration (including enum instances)
- LOCAL_VARIABLE: local variable Declaration
- METHOD: METHOD declaration
- PACKAGE: PACKAGE Declaration
- PARAMETER: PARAMETER Declaration
- TYPE: Class, interface (including annotation TYPE), or enum Declaration
|
| @ Retention |
The level at which the annotation information needs to be saved. Optional RetentionPolicy parameters include:
- SOURCE: annotation will be discarded by the compiler
- CLASS: Annotations are available in the class file, but are discarded by the VM.
- RUNTIME: the VM retains the annotation at RUNTIME. Therefore, the annotation information can be read through the reflection mechanism.
|
| @ Brief ented |
Include this annotation in Javadoc |
| @ Inherited |
Allow subclass to inherit the annotation in the parent class |
In addition, annotation elements support the following types
- All basic types (int, float, boolean, etc)
- String
- Class
- Enum
- Annotation
- Arrays of the above types
The simple notes are now over.
Annotations can also be processed with apt, used in the Observer mode, for unit testing, etc. For more details, please read chapter 20th of Java programming ideas-Annotations