Getting started with annotations in Java (Annotation) custom annotations

Source: Internet
Author: User

To drill down into annotations, we have to be able to define our own annotations and use annotations, before we define our own annotations, we have to understand the syntax of the meta annotations and the related definition annotations that Java provides for us.

Meta Note:

The function of the meta annotation is to annotate other annotations. Java5.0 defines 4 standard meta-annotation types that are used to provide descriptions of other annotation types. Java5.0 defined meta Annotations:

1. @Target,

2. @Retention,

3. @Documented,

4. @Inherited

These types and the classes they support can be found in the Java.lang.annotation package. Let's take a look at the role of each meta annotation and the instructions for using the corresponding argument.

@Target:

@Target describes the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, construction methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Using target in a annotation type of declaration can make it more explicit about the target that it modifies.

Role: Used to describe the scope of use of annotations (i.e., where the described annotations can be used)

VALUES (ElementType) are:

1.CONSTRUCTOR: Used to describe the builder

2.FIELD: Used to describe the domain

3.local_variable: Used to describe local variables

4.METHOD: Used to describe the method

5.PACKAGE: Used to describe the package

6.PARAMETER: Used to describe parameters

7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations

Use instance:

@Target (elementtype.type) public   
@interface Table {   
    /**   
     * Datasheet name annotation, default value is class name   
     * @return
    / Public String tablename () default "ClassName";   
}   
       
@Target (Elementtype.field) public   
@interface Nodbcolumn {   
       
}

Note Table can be used for annotation classes, interfaces (including annotation types), or enum declarations, whereas note Nodbcolumn can be used only for member variables of the annotation class.

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.