Definition, use, and resolution of Annotation in Java, javaannotation

Source: Internet
Author: User

Definition, use, and resolution of Annotation in Java, javaannotation

This example describes how to define, use, and parse "Annotation" in Java. Annotations are generally used in a custom development framework. For the usage reasons, we do not provide much explanation here. Here we only describe how to use annotations for backup. The following example has been tested and can run normally.

1. annotation customization.
Two annotations are defined here for annotation classes and annotation attributes respectively.

Package cc. rulian. ann; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** class annotation */@ Retention (RetentionPolicy. RUNTIME) @ Target (ElementType. TYPE) public @ interface MyTable {public String name () default ""; public String version () default "1 ";}

 

Package cc. rulian. ann; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** field comment */@ Retention (RetentionPolicy. RUNTIME) @ Target (ElementType. FIELD) public @ interface MyField {public String name () default ""; // name public String type () default ""; // type}

 

2. Use of annotations.
Describes how to use custom annotations in a class.

Package cc. rulian. ann; import java. util. date;/*** basic log **/@ MyTable (name = "T_BaseLog", version = "2") public class BaseLog {@ MyField (name = "addTime ", type = "Date") private Date log_time; // time @ MyField (name = "log_level", type = "String") private String log_level; // level @ MyField (name = "message", type = "String") private String message; // log Content public Date getLog_time () {return log_time ;} public void setLog_time (Date log_time) {this. log_time = log_time;} public String getLog_level () {return log_level;} public void setLog_level (String log_level) {this. log_level = log_level;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}

 

3. parse the annotation.
Describes how to parse annotations.

Package cc. rulian. ann; import java. lang. reflect. field;/*** read comments */public class ReadAnn {public static void main (String [] args) {// read the comments of the class BaseLog obj = new BaseLog (); // Annotation [] arr = obj. getClass (). getAnnotations (); // get all comments MyTable table = obj. getClass (). getAnnotation (MyTable. class); // get the specified comment System. out. println ("class annotation (name):" + table. name (); System. out. println ("class annotation (version):" + table. vers Ion (); // read the Comment Field [] fields = obj. getClass (). getDeclaredFields (); for (Field f: fields) {// Annotation [] arr2 = f. getAnnotations (); // get all comments MyField ff = f. getAnnotation (MyField. class); // get the specified annotation if (ff! = Null) {System. out. println ("attribute (" + f. getName () + "):" + ff. name () + "--" + ff. type ());}}}}

 

4. parse the output result.

Class annotation (name): T_BaseLog class annotation (version): 2 attribute (log_time): addTime -- Date attribute (log_level): log_level -- String attribute (message): message -- String

 

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.