Java annotation Annotation

Source: Internet
Author: User

What is annotation first?

The most common is that we use eclipse and other tools to compile JavaCodeSometimes, for example@ Deprecated,@ Override,@ Suppresswarnings. This is a common annotation.


When developing JavaProgramEspecially for Java EE applications, it is always inevitable to deal with various configuration files. Taking the typical S (Pring) S (truts) H (ibuts) architecture in Java EE as an example,Spring,StrutsAndHibernateAll three frameworks have their own configuration files in XML format. These configuration files must beSource codeSave synchronization. Otherwise, an error may occur. In addition, these errors may only be detected at runtime. It is always a bad idea to store the same piece of information in two places. The ideal situation is to maintain the information in one place. The information required for other parts is generated automatically. JDK 5 introduces the annotation mechanism in the source code. Annotations allow Java source code to not only contain functional implementation code, but also add metadata. The annotation function is similar to the annotation in the Code. The difference is that the annotation is not a description of the Code function, but an important part of implementing the Program function. Java annotations have been widely used in many frameworks to simplify the configuration of programs.

The most common of us is the three annotations mentioned above. Let's briefly introduce the functions of these three Annotations:

@ Override: it can only be used on methods. It is used to tell others that this method is used to rewrite the parent class.
@ Deprecated: It is recommended that you do not use the old APIs. Warnings are generated during compilation and can be set to all elements in the program.
@ Suppresswarnings: this type can temporarily disable some warning messages.

Open the following source files in the Java. Lang. annotation package that comes with JDK:Target. Java,Retention. Java,Retentionpolicy. Java,Elementtype. java.

The content is as follows:

 
@ Incluented @ retention (retentionpolicy. runtime) @ target (elementtype. annotation_type) Public @ interface target {elementtype [] value ();}

@ Incluented @ retention (retentionpolicy. runtime) @ target (elementtype. annotation_type) Public @ interface retention {retentionpolicy value ();}

 
Public Enum retentionpolicy {source, class, runtime}

 
Public Enum elementtype {type, field, method, parameter, constructor, local_variable, annotation_type, package}

When designing annotations, you must define a type as @ interface.

Note the following:Source, class, and runtime.

Source indicates that the annotation type information will only be stored in the program source code. If the source code is compiled, the annotation data will disappear and will not be stored in the compiled data. class file.
Class indicates that the annotation type information is stored in the program source code and compiled. in the class file, this information is not loaded into the Virtual Machine (JVM) during execution. note that when you do not set a retention value of the annotation type, the default value is class.
Runtime indicates that information is retained in the source code and compiled. class files. During execution, this information is loaded into the JVM.

@ Target the elementtype is used to specify which elements the annotation type can use. description: type, field, method, parameter, constructor, local_variable, annotation_type, package. The type can be used in the class, interface, Enum, and annotation types.
In addition, @ target uses itself to declare itself.If an annotation type does not specify the elements on which @ target is used, it can be used on any element. The elements here refer to the above eight types.
Here are a few correct examples:

@ Target (elementtype. Method) @ target (value = elementtype. Method) @ target (elementtype. method, elementtype. constructor)

@ Brief ented: The purpose of this annotation type information can be displayed in the javaapi instruction document, this type of generated information cannot be found when you use javadoc to generate an API document.

In addition, if you need to inherit the annotation data to the subclass, the annotation type @ inherited will be used.

This article just briefly describes the common usage of annotations. For more in-depth annotation learning, seeArticleReferences at the end. Next let's look at a custom annotation: the source code has the following:

Source code:

 
Package COM. java. annotation; 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 myannotationclass {Public String MSG ();}

Package COM. java. annotation; import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target;/*** method annotation **/@ retention (retentionpolicy. runtime) @ target (elementtype. method) Public @ interface myannotationmethod {Public String common ();}

 
Package COM. java. annotation; import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target; @ retention (retentionpolicy. runtime) @ target (elementtype. field) Public @ interface myannotationfield {Boolean request ();}

Package COM. java. annotation; @ myannotationclass (MSG = "this is a class annotation") public class myannotationdemo {public myannotationdemo () {} public myannotationdemo (string Hello) {This. hello = Hello;} @ myannotationmethod (Common = "this is a method annotation") Public void method () {system. out. println (Hello) ;}@ myannotationfield (request = true) Private string hello ;}

Package COM. java. annotation; import Java. lang. reflect. field; import Java. lang. reflect. method; public class myannotationtest {public static void main (string [] ARGs) {myannotationdemo = new myannotationdemo ("Hello rollen"); myannotationclass annotationclass = demo. getclass (). getannotation (myannotationclass. class); system. out. println (annotationclass. MSG (); Method method = NULL; try {method = demo. getclass (). getmethod ("method", new class [0]);} catch (securityexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace ();} myannotationmethod annotationmethod = method. getannotation (myannotationmethod. class); system. out. println (annotationmethod. common (); field = NULL; try {field = demo. getclass (). getdeclaredfield ("hello");} catch (securityexception e) {e. printstacktrace ();} catch (nosuchfieldexception e) {e. printstacktrace ();} myannotationfield annotationfield = field. getannotation (myannotationfield. class); system. out. println (annotationfield. request ());}}

References: http://www.infoq.com/cn/articles/cf-java-annotation

Http://tiantian0521.blog.163.com/blog/static/4172088320118243436208/

 

 

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.