This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
Java annotations
Java annotations, also known as Java annotations, is an Annotation mechanism introduced in JDK5.0.
Classes, methods, variables, parameters, and packages in the Java language can all be annotated. Unlike Javadoc, Java annotations can be retrieved through reflection. Annotations can be embedded in bytecode when the compiler generates class files. The Java virtual machine can retain the annotations and get them at run time. Of course, it also supports custom Java annotations.
Annotations, like reflection, are one of the most important and often forgotten aspects of Java. Even though Spring, SpringMVC, SpringBoot, and other frameworks are full of annotations, we selectively ignore them. Many people don’t understand how it works, and some even confuse it with comments… The work is also mechanically adding @RequestMapping to the Controller. Yes, we’re so used to it that we think it should be that way.
The content is introduced
- The role of annotations
- The nature of annotations
- Reflect annotation information
- Yuan notes
- Custom annotations
The role of annotations
- Format checking: Tells the compiler that the IDE will report an error if a method marked with @override is not a method of the parent class.
- Reduce configuration: run time dynamic processing, get annotation information, to achieve the function of replacing configuration files;
- Reduce rework: third-party framework xUtils, for example, reduces calls to findViewById by annotating @viewinject (JUnit, ActiveAndroid, etc.);
The nature of annotations
“Java. Lang. The annotation. The annotation” the interface have so a word, used to describe the “annotation”.
The common interface extended by all annotation types All annotation types are inherited from this common interfaceCopy the code
The definition of JDK built-in annotations:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy the code
This is the definition of @override, which is essentially:
public interface Override extends Annotation{
}
Copy the code
An annotation is like a label that is attached to program code for another program to read. So the relationship is: define annotations, use annotations, and read annotations
Reflect annotation information
Reflection to get annotation information:Console: Throw exception – the annotation MyAnnotation is not reserved for reflection.
As we said, annotations are essentially an interface that inherits the Annotation interface, and when you get an Annotation class instance through reflection, which is our getAnnotation method here, the JDK actually generates a proxy class that implements our Annotation using a dynamic proxy mechanism.
Yuan notes
A meta-annotation is an annotation that decorates an annotation, usually used in the definition of an annotation, for example:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy the code
- @target: Added to an annotation to limit where the annotation can be used. If you want to limit where an annotation can be used, you can use it on a custom annotation. We can default this time, no special restrictions.
- @target: The action Target of the annotation
- @Retention: The lifecycle of annotations
- Documented: Whether an annotation should be included in a JavaDoc document
- Inherited: Allows subclasses to inherit this annotation
Custom annotations
When using the @ interface custom annotations, automatic inherited Java. Lang. The annotation. The annotation interface, the compiler automatically other details. When an annotation is defined, it cannot inherit from another annotation or interface. @interface is used to declare an annotation, where each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the type of the returned value is the type of the parameter. (The type of the returned value can only be basic, Class, String, or enum.) You can declare the default value of the parameter by default.
Custom annotation format:
Public @interface annotation name {body; }Copy the code
- Note the data types supported by the parameter:
1, all basic data types (int, float, Boolean, byte, double, char, long, short).
2. String;
3, Class type;
4. Enum type;
5. Annotation type;
6. Arrays of all of the above types.