In current development, annotations are increasingly applied to development. Both the new features of servlet3.0 and the full annotations in SSH bring great convenience and intuition to development. therefore, you must have a deep understanding of the annotations.
I. Custom Annotation
Target of custom annotation: information originally written in the configuration file, which can be described through Annotation
Define annotation usage@ InterfaceKeywords
Configuration File Information, which is used in the AnnotationAttributeDescription
Example: string name ();
Default Value: string name () default "XXX"
Annotation attributes can only be of the following types:
String type
8 Basic Data Types
Class type
Enumeration type
Annotation type
The preceding one-dimensional array
Special attribute value: If the annotation has an attribute named value, you can omit the value = part when using the annotation, for example, @ myannotation ("XXX ")
Special attribute value [];
Ii. JDK meta-Annotation
$2.1 annotation indicates annotation for annotation Modification
The following meta-annotation is defined in JDK:
@ Retention: Can be used to modify only one annotation definition. It is used to specify the domain that can be retained by this annotation. @ resolve ention contains a retentionpolicy member variable, which specifies the domain.
Retentionpolicy. class:The compiler records the annotation in the class file. When a Java program is running, the JVM does not keep the annotation. This is the default value.
Retentionpolicy. Runtime: The compiler will record the annotation in the class file. When a Java program is running, the JVM will keep the annotation. The program can obtain the annotation through reflection.
Retentionpolicy. Source:The compiler directly discards the comment of this policy.
2.2
@ Target: Specifies which member of the annotation is used to modify the class. @ target contains a member variable named value and of the elementtype type.
@ Incluented: specifies that the annotation class modified by the meta-annotation will be extracted as a document by the javadoc tool.
@ Inherited: The annotation modified by it will be inherited. If a class uses the annotation modified by @ inherited, its subclass will automatically have this Annotation
3. parse annotation information
JDK 5.0 adds the annotation interface under the java. Lang. Reflect package, which indicates that the annotation is acceptable in the program.
When an annotation type is defined as runtime annotation, This annotation is visible during runtime. When the class file is loaded, the annotation stored in the class file will be read by the virtual machine.
The program can call the following method of the annotationelement object to access annotation information:
4. Use XML files or annotations for actual development:
According to the actual development requirements, if the scalability of the Project is taken into account, the XML file must be used. If the convenience of the Development Project is taken into account, annotations should be used.
Advantages and disadvantages:
1. the XML document does not need to re-compile the Java class to modify the configuration. However, the XML document is often too large and the configuration is cumbersome and not intuitive, which can easily interrupt developers' ideas.
2. The annotations are simple, convenient, and intuitive. However, you need to re-compile the Java class during configuration modification.
The advantages and disadvantages of XML files and annotations can be measured in actual development.