Summary of spring annotations and Java meta annotations

Source: Internet
Author: User

Note Annotation

Note-based development, which makes the code concise, readable, simplified configuration, but also improve the efficiency of development, especially the rise of springboot, with the start-up and automatic configuration of the perfect, it is based on the development of annotations to new heights.

Meta Annotations Meta-annotation

Java 5 defines four standard meta-annotation types to provide functional descriptions of other annotations.

Located under the Java.lang.annotation package, respectively:

1. @Target

2. @Retention

3. @Documented

4. @Inherited

Take @profile annotations as an example:

@Target ({elementtype.type, elementtype.method}) @Retention (retentionpolicy.runtime) @Documented @conditional ( Profilecondition. class ) public @Interface Profiles {    /**     * The set of profiles for which the annotated component should be registered.      */     string[] Value ();}
@Target

Describes the range of objects that the annotation modifies.

Annotations can be used for: package, type (class, interface, enumeration, annotation), type members (methods, construction methods, member variables, enumeration values), method parameters, and local variables (loop variables, catch parameters).

According to the scope of action, the common annotations given by the individual, according to the scope of action, various types of temporary lift one:

@Configuration, @MapperScan, @RestController, @RequestMapping, @ResponseBody, @Autowired, @Resource, @Value, @ Pathvariable ...

The specific value range is ElementType (enum) type of array, see source code:

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  Target {    /**     * Returns An array of the kinds of element s an annotation type     * can is applied to.      @return An array of the kinds of elements a annotation type     * can be applied     to*/    Ele Menttype[] Value ();}
    1. Elementtype.constructor: Description Constructor
    2. Elementtype.field: Description Domain
    3. Elementtype.localvariable: Describing local variables
    4. Elementtype.method: Description method
    5. Elementtype.package: Description Package
    6. Elementtype.parameter: Description parameter
    7. Elementtype.type: Description class, interface, enum, annotation
@Retention

Defines the duration of the annotation (source file, bytecode, run time).

To illustrate the extent to which the annotations described are in effect.

The value is unique, that is, one of the specific save policies, Retentionpolicy (enum).

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  Retention {    /**     * Returns the Retention policy.      @return the retention policy     *    /Retentionpolicy value ();}

Retentionpolicy includes:

    1. Source: Original File valid
    2. CLASS: Byte-code file is valid
    3. Runtime: Valid at run time
@Documented

markup annotations, which can be documented by tools such as Javadoc, which describe annotations of that type, should be used as public APIs for annotated program members, and no members

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  documented {}
@Inherited

Tag annotations, which describe annotations that can be inherited. That is, if a class is decorated with annotations of the @inherited tag, the annotation also works on subclasses of the class.

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  inherited {}

Summary of spring annotations and Java meta annotations

Related Article

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.