Http://www.jb51.net/article/55371.htm
retention annotations
Retention (reserved) Note that this type of annotation is retained at that stage. There are three values:
1.retentionpolicy.source--This type of annotations is preserved only at the source level and is ignored at compile time
2.retentionpolicy.class--This type of annotations is preserved at compile time and exists in the CLASS file, but the JVM ignores
3.retentionpolicy.runtime--This type of annotations will be retained by the JVM, so they can be read and used by the JVM or other code that uses the reflection mechanism at runtime.
Example 5 demonstrates the declaration of Retentionpolicy.runtime:
Example 1 for Java annotations:
@Retention (retentionpolicy.runtime) public @interface test_retention { String dotestretention ();}
In this example, the @Retention (retentionpolicy.runtime) Note indicates that the test_retention annotation will be retained by the virtual machine so that it can be read by reflection at run time.
Documented annotations
Documented annotations indicate that this annotation should be recorded by the Javadoc tool. By default, Javadoc is not included in annotations. However, if a @Documented is specified when the annotation is declared, it is processed by a tool such as Javadoc, so the annotation type information is also included in the generated document. Example 6 further demonstrates the use of @Documented:
Example 2 for Java annotations:
@Documentedpublic @interface test_documented { String dotestdocument ();}
Next, modify the Testannotations class as follows:
public class Testannotations {public static void Main (String arg[]) { new testannotations (). Dosometestretention (); New Testannotations (). dosometestdocumented (); } @Test_Retention (dotestretention= "Preserve annotation information test") Public void Dosometestretention () { System.out.printf ("Test note type ' Retention '); } @Test_Documented (dotestdocument= "Hello document") Public void dosometestdocumented () { System.out.printf ("Test note type ' documented ');} }
Now, if you use the Javadoc command to generate the testannotations.html file, you will see a result similar to Figure 1.
As you can see, there is no annotation-type information () method for the Dosometestretention () method in the document. However, the documentation for the Dosometestdocumented () method provides descriptive information about the annotations. This is because the @Documented tag is added to the test_documented annotation. The previous note, test_retention, does not specify @Documented tag (tag).
inherited annotations (this may be a problem ...)
This is a slightly more complex type of annotation. It indicates that the annotated class is automatically inherited. More specifically, if you define annotations with a @Inherited tag and then label another parent class with a defined annotation, and the parent class has a subclass (subclass), all the properties of the parent class are inherited to its subclasses. In Example 7, you'll see the benefits of using @Inherited tags.
Example of Java Annotations 3
First, define your annotations:
@Inheritedpublic @interface Myparentobject { boolean isinherited () default true; String dosomething () default "do what?";}
Next, you annotate a class with annotations:
@MyParentObjectpublic Class Mychildobject {}
As you can see, you don't need to define an interface method in an implementation class. Because @Inherited tags are used, these are automatically inherited. What would it look like if you used an ancient way to define the implementation class? Take a look at this old way of realization:
public class Mychildobject implements Myparentobject {public boolean isinherited () { return false; } Public String dosomething () { return ""; } public boolean equals (Object obj) { return false; } public int hashcode () { return 0; } Public String toString () { return ""; } Public Class Annotationtype () { return null; }}
See the difference? As you can see, you must implement all the methods of the parent interface. In addition to isinherited () and the Myparentobject dosomething () method, you also need to implement the Equals (), toString (), and Hascode () methods of Java.lang.Object. There are also Annotationtype () methods for the Java.lang.annotation.Annotation class. Whether you want to implement these methods or not, you must include them in the inherited object.
Spring boot: Annotations @Documented annotations @Inherited @Retention annotations