Review java Annotation (Annotation)

Source: Internet
Author: User

Recently, many frameworks are implemented through annotations. To deepen your memory, review the annotations.


Annotations are not annotations. -- However, the java language has three built-in Annotations: @ Override, @ Deprecated, and @ SuppressWarnnings. The first is to notify the compiler to perform method override checks; the second is to remind programmers to use outdated methods; and the third is to notify the compiler to ignore warnings. These three built-in annotations give people the same feeling as annotations. In fact, the usage of annotations is far more simple than that. Using custom annotations can help us to better understand them.


The purpose of using custom annotations is to add some tags to the Program (which can also be called metadata), which are used by other programs that need attention. If you don't pay attention to it, you can ignore it. Therefore, the complete process of using annotations includes three steps: defining annotations, using annotations, and parsing annotations. It is the same as defining classes, creating objects, and using objects. The following is a small example:

Step 1: Define annotations

Annotation (Annotation) is actually an interface, but it uses @ interface instead of the interface keyword to show the difference. Because it is indeed different from a common interface-Annotation-type methods must be non-parameter-free and thrown without exceptions. By default, they are inherited from java. lang. annotation. Annotation


First define an annotation for the class

Package com. annotation. zjc; import java. lang. annotation. *; // The following three lines are "meta annotation", which is used to indicate the life cycle, scope of use, and docable @ Retention (RetentionPolicy. RUNTIME) @ Target (ElementType. TYPE) @ Documentedpublic @ interface Description {String value ();}

Define another annotation for constructor and Method

Package com. annotation. zjc; import java. lang. annotation. *; @ Retention (RetentionPolicy. RUNTIME) @ Target ({ElementType. CONSTRUCTOR, ElementType. METHOD}) @ Documentedpublic @ interface Resource {int id (); String name (); String type () default "Object"; // default value}

Step 2: Use annotations

Annotations can be used in the parameters of packages, classes, fields, methods, methods, and local variables (at most, they are used in classes and methods, and are rare in other locations ). Using annotations is equivalent to creating an annotation class instance.

Package com. annotation. zjc; @ Description ("Inject use annotation") // equivalent to @ Description (value = "Inject use annotation"), the field name is value, you can use the abbreviated public class ApplyAnnotation {@ Resource (id = 1, name = "zhang", type = "person") public ApplyAnnotation () {// init ...} public void execute (Resource r) {System. out. println (r. id () + "" + r. name () + "" + r. type ());}}

Step 3: parse the Annotation

Package com. annotation. zjc; import java. lang. reflect. constructor; public class testMain {public static void main (String [] args) {ApplyAnnotation aa = new ApplyAnnotation (); Class <?> Objclass = null; try {objclass = Class. forName ("com. annotation. zjc. ApplyAnnotation");} catch (Exception e) {}if (objclass! = Null & objclass. isAnnotationPresent (Description. class) {Description des = objclass. getAnnotation (Description. class); // obtain the "annotation object" System. out. println (des. value ();} Resource r = null; if (objclass! = Null) {Constructor <?> Cons [] = objclass. getConstructors (); // get the Constructor of the class through reflection <?> Con: cons) {if (con. isAnnotationPresent (Resource. class) {r = (Resource) con. getAnnotation (Resource. class); // obtain the "annotation object" }}} if (r! Export null)aa.exe cute (r );}}

In actual situations, the first and third steps are usually done by the framework or tool program. We only need to use annotations in our own program. Of course, you must import the corresponding package file before use.

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.