Java Custom Annotations

Source: Internet
Author: User

Java annotations are some of the meta-information that is attached to the code, used by some tools to parse and use at compile and run time, to illustrate and configure functionality.
Annotations do not and do not affect the actual logic of the code, only the ancillary role. Included in the Java.lang.annotation package.

1, Yuan annotation

Meta annotations are annotations of annotations. including @Retention @Target @Document @Inherited four kinds.


1.1. @Retention: Define retention policies for annotations

@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file@Retention (Retentionpolicy.class)//default retention policy, annotations exist in the class bytecode file, but are not available at run time .@Retention (Retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time1.2. @Target: Define the purpose of the annotation the source code for its definition is:@Documented@Retention (retentionpolicy.runtime)@Target (Elementtype.annotation_type)public @interface Target {
    1. Elementtype[] Value ();

}

@Target (Elementtype.type)//interfaces, classes, enumerations, annotations@Target (Elementtype.field)//fields, constants for enumerations@Target (Elementtype.method)//Method@Target (Elementtype.parameter)//Method parameters@Target (Elementtype.constructor)//Constructors@Target (elementtype.local_variable)//Local variables@Target (Elementtype.annotation_type)//Annotations@Target (Elementtype.package)/// packageBy the above source code can know, his elementtype can have multiple, an annotation can be class, method, field and so on 1.3, @Document: Explain that the note will be included in Javadoc 1.4, @ Inherited: The description subclass can inherit this annotation from the parent class

For specific examples, refer to: http://blog.csdn.net/yixiaogang109/article/details/7328466

Java Custom 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.