Implementation mechanism of Java annotations

Source: Internet
Author: User

I. What is an annotation:

Annotations are tokens, or they can be interpreted as a special modifier applied to classes, methods, parameters, properties, and constructors. The annotations have the following three kinds of functions:

The first type: Generate documents, commonly used @[email protected] and so on.

Second: The role of the alternative configuration file, especially in some frameworks such as spring, the use of annotations can reduce the number of configuration files a lot.

The third type: Check the format of the code, such as @override, to identify whether a method overrides its parent class.

Two. The underlying implementation principle of annotations:

The bottom of the annotation is also implemented using reflection, and we can customize a note to understand it. Annotations and interfaces are somewhat similar, but it is stated that the annotation class needs to be @interface, that the annotation class supports only basic types, string and enumeration types, that all properties are defined as methods, and that default values are allowed.

Starting with Java 5.0, there are four meta-annotations in java.lang.annotations that specifically annotate other annotations: @Target (where the annotations are used), @Retention (annotation run state), @Documented (Generate documentation, Add an explanation of the class), @Inherited (allows subclasses to inherit annotations from the parent class)

@Target? --where the annotations are used

Type,//Give class (type) annotations
field,//annotate field, do not forget that fields can be objects
method,//annotation of methods
PARAMETER,//Give parameter annotations
CONSTRUCTOR,//Annotation for construction method
local_variable,//give local variable annotations
annotation_type,//annotations (which seems to be inappropriate for the class)
Package,//packet annotation

@Retention--Annotation run status

source,//source state running,
class,//compile-time file run
Runtime//Run time

@Documented--Generate a description document, add an explanation of the class

@Inherited--allows subclasses to inherit annotations from the parent class.

Reference code:

@Target ({elementtype.constructor, Elementtype.method, Elementtype.parameter, Elementtype.field, Elementtype.annotation_type})
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface autowired {

/**
* Declares whether the annotated dependency is required.
* <p>defaults to {@code true}.
*/
Boolean required () default true;

}

Boolean required () default true; This code indicates that the default value of the class is true

personal Opinion: (unsure of the right or not) beans we call annotations (spring annotations) The corresponding code block with annotations loaded in the code is framed (just like a hate tag in the game), and the program accesses the URL by using the following procedure 1, the framework 2, match the URL in the controller annotations (via the annotated Value or path)3, match the relevant interface in the serviceannotations 4-- products ) and the annotation function implementation is a reflection mechanism (rough view) that is based on the reflection mechanism of Java: Invoking a class (for example, a tool class) can obtain any property or method that uses the class.
a view of oneself









Implementation mechanism of Java annotations

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.