In-depth understanding of Java Annotations (Annotation) republication

Source: Internet
Author: User

Recent projects need to do permission validation, experts wrote a framework of authorization verification, read the code, or there is a gap with the master AH ~

The permission frame uses some annotations to reflect what, so look back to the previous article, "in-depth understanding of Java annotations (Annotation)", turn around, do some additional, do the memo use.

First, what is an annotation?

Too lazy to play, make a wiki explanation, see it yourself (may be translated before, now lazy)

How to use annotation and customize annotations

First sweep the blind, the yuan annotation is annotation's meta-annotations a total of 4, respectively: @Target, @Retention, @Documented,@Inherited

1, @Target used to represent the scope of the declared annotation adornment, its ElementType enumeration has the following several

Simply translate it.

annotation_type Annotation Type declaration

CONSTRUCTOR constructor Method declaration

FIELD Property Declaration

local_variable Declaration of local variables

Method declaration

Package Declaration

PARAMETER parameter declaration

Type class, interface (including annotation type), or enum-type declaration

2, @Retention shows how long the annotations can be retained, or at what stage, the default save-stay scheme is class, and its Retentionpolicy enumeration has the following

Simply translate it.

Class annotations are recorded in the class file, but are not persisted in the virtual machine at run time (that is, the compile phase is valid)

runtime annotations are recorded in the class file and are retained when the virtual machine is running, so it can be read by the reflection mechanism (which is still valid at run time)(which is what we usually use)

Source is discarded by the compiler at compile time (that is, the source phase is valid), and several common annotations in Java that are used to compile errors in the code phase are used with this value: @Override, @Deprecated, @SuppressWarnings

3, @Documented the description of the annotation can be documented. (Nothing to say)

4. @Inherited indicates that the type of annotations is automatically inherited. If an annotation uses the @inherited meta-annotation, its subclasses will also automatically add the annotation (that is, the annotation can be inherited).

5, jdk1.5 after the reflection of the entities have implemented the Annotatedelement interface, making it easier to read annotations, in order to save time, or to cut the JDK document Diagram bar

Ii. What is the use of annotations

The literal meaning of the note is to explain it.

The purpose of Java is to annotate classes, methods, and properties, such as routing, permissions, and so on for this method/class.

(The above are their own yy)

Third, how to use the annotations

The use of annotations is generally categorized in three steps: defining annotations, using annotations, and parsing annotations. Words don't say much, on the code.

1. Define Annotations

2. Use annotations

3. Analytical annotations

Reference article:

In-depth understanding of Java annotations (Annotation)

Oracle Java API (This section is going to choose the class under the annotation package)

The Java? Tutorials Lession:annotaions

Java annotations (Baidu Encyclopedia)

In-depth understanding of Java Annotations (Annotation) republication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.