[JavaSE] annotation-custom annotation and javase annotation Customization

Source: Internet
Author: User

[JavaSE] annotation-custom annotation and javase annotation Customization

Annotation classification:

Source code annotation

@ Override annotated with JDK during compilation

@ Autowired of Spring annotation during runtime

 

Syntax requirements for custom annotations

① Use the @ interface keyword to define the Annotation

② The Member is executed without any parameters or exceptions.

③ You can use defaut to specify a default value for the member.

④ The member type is restricted, including the original type and String Class Annotation Enumberation

⑤ If the annotation has only one member, the member name must be named value (). during use, the member name and value assignment number (=) can be ignored)

⑥ The annotation class can have no members, which is called the identifier annotation.

Package com. tsh. ano; import java. lang. annotation. documented; import java. lang. annotation. elementType; import java. lang. annotation. inherited; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; // meta annotation @ Target ({ElementType. METHOD, ElementType. TYPE}) // scope @ Retention (RetentionPolicy. RUNTIME) // life cycle @ Inherited // can be Inherited @ incluented // generate document public @ interface Description {String desc (); String author (); int age () default 18;} // use the annotation class DescAnoDemo {@ Description (author = "taoshihan", desc = "hahaha") void show (){}}

 

Meta annotation: Annotation

Use custom annotations

Parse Annotation

 

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.