Understanding java annotation one

     Everyone should be familiar with java annotations, that is, Annotation. As long as you are involved in the back-end development of java, then you must have been exposed to spring. Many functions in the spring framework are implemented using annotations. For example: the most common @Autowired, inject the objects in the spring container into the class, and use it directly, no need to create a certain class frequently; there is also @RequestMapping, through this annotation, the processor handler in spring matches the front end. The request is then entered into the executing controller. Regarding annotations, many people usually use it very well. Just a @, many things do not need to be configured or written by themselves, but I really want to explain the annotations and the realization, and I feel very confused.

Let's first look at the official explanation of the comment:

从JDK5开始,Java增加对元数据的支持,也就是注解,注解与注释是有一定区别的,可以把注解理解为代码里的特
殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过注解开发人员可以在不改变原有
代码和逻辑的情况下在源代码中嵌入补充信息。

For such an explanation, there is generally only one word "halo". And maybe many people don't even have the desire to read. Generally speaking, whenever we explain an abstract noun, we will compare it with something commonly used in real life in order to better understand it. Then the question is, what is the similarity of annotations?

For those of us who often use annotations, we all know that after adding annotations, the code can be identified, and then the corresponding verification, comparison or injecting the class, that is to say, the annotation is like a sign, put there, you will know as soon as you see it "Hey, that's a comment, mark verification!"

Therefore, everyone generally compares the annotations with a sign or a label, which is eye-catching and easy to identify, which makes it easier to understand.

Of course, in this comparison, some functions of annotations may not be completely analogized, so I thought about it again and found a thing that is very similar to annotations-that is, the packaging box!

First of all, there must be a label on the box, reminding you what it is and what it does. This is similar to a label. Secondly, the packaging boxes nowadays are more exquisite, such as moon cake boxes. Generally, the same kind of box can only contain one kind of moon cake. If other types are packed, it may be larger or smaller, which is not suitable, wastes space, and makes consumers feel Uncomfortable.

Let's take a look at the annotations. The first is the logo, to make it clear that the role of this annotation is similar to the product introduction on the box. The second is the function. Annotation is generally used for method or attribute verification or comparison, and the unique shape of the packaging box is the verification of foreign objects. Comparisons and inconsistencies are not allowed to be put in! It's like when requesting a method or getting the value of an attribute, there is no way to enter the method or get the value of an attribute if this annotation is not met.

Well, let's talk about common java annotations below.

First of all, let's start with the most familiar @Autowired annotation.

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER,
 ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}

You can see that the annotations in java use @interface, and there are three annotations above the @Autowired annotation, so what is this?

Speaking of this, we must first talk about meta-annotations, so what is meta-annotation?

Meta-annotation is an annotation that can be annotated to an annotation, or meta-annotation is a basic annotation, but it can be applied to other annotations. In other words, another layer of packaging is added to the box.

There are 5 types of meta tags: @Retention, @Documented, @Target, @Inherited, and @Repeatable.

@Retention

Retention means retention period in English. When @Retention is applied to an annotation, it explains the survival time of the annotation.

Its value is as follows:

    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, and it will not be loaded into the JVM.
    The RetentionPolicy.RUNTIME annotation 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.

@Document

As the name suggests, this meta-annotation must be related to the document. Its function is to be able to include the elements in the annotations into the Javadoc.

Target means a target, and @Target specifies where the annotation is applied.

You can understand that when an annotation is annotated by @Target, the annotation is limited to the application scenario.

Analogy to the packaging box, the original packaging box is where you want to post it to wherever you want, but because of the existence of @Target, its use is very specific, such as packaging methods, classes, method parameters, and so on. @Target has the following values

    ElementType.ANNOTATION_TYPE can annotate an annotation
    ElementType.CONSTRUCTOR can annotate construction methods
    ElementType.FIELD can annotate attributes
    ElementType.LOCAL_VARIABLE can annotate local variables
    ElementType.METHOD can annotate methods
    ElementType.PACKAGE can annotate a package Annotate
    ElementType.PARAMETER to annotate parameters in a method
    ElementType.TYPE Annotate a type, such as class, interface, enum

@Inherited

Inherited means inheritance, but it does not mean that the annotation itself can be inherited, but that if a superclass is annotated by @Inherited annotations, then if its subclass is not applied by any annotations, then this subclass The class inherits the annotations of the super class

@Repeatable

Repeatable naturally means repeatable. @Repeatable was only added in Java 1.8, so it is a new feature.

What kind of annotation will be applied multiple times? Usually, the value of the annotation can be multiple at the same time.

For example, a person is both a programmer and a product manager, and he is also a teacher.

That's it for today, next time I will talk about simple acquisition of annotations (via reflection).

Guess you like

Origin blog.csdn.net/zsah2011/article/details/90610600