tags: java basics java
Java Annotation, also known as Java annotation, is an annotation mechanism introduced by JDK5.0.
Classes, methods, variables, parameters, and packages in the Java language can be marked. In layman's terms, mark classes, methods, variables, and parameters, and then you can use reflection Get the annotation content. When the compiler generates the class file, the annotations can be embedded in the bytecode. The Java virtual machine can retain the annotation content, and the annotation content can be obtained at runtime. Of course it also supports custom Java annotations.
Java defines a set of annotations, there are 7 in total, 3 in java.lang, and 4 in java.lang.annotation.
Notes on the code:
@Override-Check whether the method is an overridden method. If there is no such method in the parent class or the referenced interface, a compilation error will be reported.
@Deprecated-mark deprecated methods. If you use this method, a compilation warning will be reported.
@SuppressWarnings-Instructs the compiler to ignore warnings declared in comments.
Annotations that act on other annotations (also known as meta annotations):
@Retention-Identifies how this annotation is saved, whether it is only in the code, compiled into the class file, or can be accessed through reflection at runtime.
@Documented-mark whether these comments are included in the user document.
@Target-mark what kind of Java member this annotation should be.
@Inherited-mark which annotation class this annotation is inherited from (the default annotation is not inherited from any subclass)
Starting from Java 7, 3 additional annotations have been added:
@SafeVarargs-Java 7 began to support, ignoring any warnings generated by methods or constructor calls that use generic variables with parameters.
@FunctionalInterface-Supported in Java 8, which identifies an anonymous function or functional interface.
@Repeatable-Java 8 started to support, marking that an annotation can be used multiple times on the same statement.

Created custom annotation

package com.example.springboot.Demo;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
/* This annotation is used to define the life cycle of a custom annotation, where the value value is the following three values in the RententionPolicy enumeration class:
The RetentionPolicy.SOURCE annotation is only reserved in the source code phase, and it will be discarded and ignored when the compiler compiles.
The RetentionPolicy.CLASS annotation is only retained until compilation is in progress, it will not be loaded into the JVM.
-RetentionPolicy.RUNTIME annotations can be retained until the program is running, it will be loaded into the JVM, so they can be obtained when the program is running*/
@Documented //Its function is to be able to include the elements in the annotation into the Javadoc.
@Target(ElementType.TYPE)
/*
@Target means that Annotation can be used to modify those elements. If it is not marked, it means that all elements can be modified. The range of values is as follows:
ElementType.TYPE: modified class, interface, enumeration
ElementType.FIELD: Modification attribute
ElementType.METHOD: Modification method
ElementType.PARAMETER: Modification method parameters
ElementType.CONSTRUCTOR: modified construction method
ElementType.LOCAL_VARIABLE: Modification of local variables
ElementType.ANNOTATION_TYPE: Modification annotation
ElementType.PACKAGE: decoration package
ElementType.TYPE_PARAMETER: Generic parameters (added in 1.8)
ElementType.TYPE_USE: where the type is used (added in 1.8)
*/
@Inherited
/*
@Inherited indicates that a marked type is inherited. When the annotation type modified with @Inherited is used in a class, the annotation will be used in the corresponding subclass of the class.
*/
@Repeatable(MyAnnotaions.class)
/* @Repeatable is used to define whether custom annotations can be reused. The value in MyAnnotaions defines a MyAnnotation array
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface MyAnnotaions {
MyAnnotation [] value() ;
}
When not using @Repeatable annotation, you want to use annotation writing multiple times:
@MyAnnotaions({
@MyAnnotation(name = "Zhang San", age = 18),
@MyAnnotation(name = " ", age = 20)
})
public class AnnotationDemo {}
When using @Repeatable annotation, you can write like this
@MyAnnotation(name = "Zhang San", age = 18)
@MyAnnotation(name = " ", age = 20)
public class AnnotationDemo {}
This method not only looks concise, when using reflection to obtain, you can also directly use MyAnnotation.class to obtain the annotation array collection
*/
public @interface MyAnnotation {
String name() ;
int age();
}
package com.example.springboot.Demo.controller;
import com.example.springboot.Demo.MyAnnotation;
@MyAnnotation(name = "Zhang San",age = 18)
public class AnnotationDemo { //This class is our ordinary class
public static void main(String[] args) {//I just wrote a main method here, just for visit
System.out.println("");
}
}
The annotation is just a label for a certain class or method, and the real explanation of the true meaning of the label is to obtain the annotation in the reflection later, and perform detailed business logic processing on the annotation.
One, @Documented The @Documented annotation indicates that this annotation will be recorded by tools such as javadoc, that is, the annotation type information will also be included in the generated do...
Java Annotations (1): Introduction to annotations and introduction to custom annotations table of Contents An introduction to Java annotations and introduction to custom annotations table of Contents ...
Java annotation (Annotation), also known as Java annotation, is a kind of annotation mechanism introduced by JDK5.0. Classes, methods, variables, parameters and packages in the Java language can be ma...
Foreword: App project development is mostly based on UI pages. At this time, we need to call a lot of codes such as findViewById and setOnClickListener. We can accept it when there are few controls. W...
Article Directory JDK four annotations Annotation format Meta annotation Annotation attributes Parse annotation Code example Annotation case JDK four annotations @Override: Check whether the annotatio...
advantage: 1. A controller class handles multiple actions (this method cannot be implemented if the Controller is implemented). 2. No need to configure to the xml file, it will cause the xml file to b...
In the persistence layer, business layer, and control layer, @Repository, @Service, and @Controller are used to gaze at the classes in the layer, while @Component is used to gaze at the more neutral c...
1. java annotation introduction Describe the annotation in one word, that is, metadata, which is the data that describes the data. Simply put, it is the "comment description" of the java sou...
Today, talk about annotations, signs @. For the different versions of java, the annotation appears in jdk1.5 but in the jdk1.5 version using annotations must continue the rewriting of the method of th...
Custom annotation @Target @Retention @Document @Inherited Create a custom annotation Define an interface Interface implementation The output of the operation is as follows: reference:Java custom annot...