Common annotations in Java

Source: Internet
Author: User

Common annotations in Java
JDK annotation @ Override @ Deprecated @ Suppvisewarnings
Common third-party Annotations: Spring: @ Autried @ Service @ Component @ Repository Mybatis: @ InsertProvider @ UpdateProvider @ Options
Annotation classification: according to the operating mechanism, it is divided into: source code annotation: only in the source code, compiled into a class file will be gone. Annotation during compilation: both the source code and the class file exist. For example, jdk has built-in annotations. Runtime annotation: annotations that work in the runtime stage and even affect the runtime logic. Such as autowired.
Meta annotation: annotation.
Custom annotation: These annotations are meta annotations, and their functions are described as follows @ Target (ElementType. METHOD, ElementType. TYPE) custom annotation scope definition: Where METHOD is declared on the METHOD, there are CONSTRUCTOR (CONSTRUCTOR), FIELD (FIELD Declaration), LOCAL_VARIABLE (local variable Declaration), PACKAGE (PACKAGE Declaration), PARAMETER (PARAMETER Declaration), TYPE (class or interface declaration) @ Retention (RetentionPolicy. RUNTIME) custom lifecycle definition: RUNTIME (which exists at RUNTIME and can be read through reflection), CLASS (which is recorded in class during compilation and ignored during RUNTIME), SOURCE (only in the SOURCE code display, will be discarded during compilation) @ Inherited custom annotation can inherit from quilt class: With this annotation, it indicates that subclass inheritance is allowed, the annotation for the parent class is valid. It applies only to class inheritance, and the interface implementation is invalid. (Personal inspiration: You can use annotation inheritance to verify common interface parameters .) @ Documented: The javadoc generated by Documented contains the annotation.
Parse annotation: Concept: obtains runtime annotation information on classes, functions, or members through reflection, so as to dynamically control the logic when the program runs.
The Code is as follows:

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; @ Target ({ElementType. METHOD, ElementType. TYPE}) @ Retention (RetentionPolicy. RUNTIME) @ Inherited @ incluentedpublic @ interface Decription {String desc (); String author () default; // if you do not specify the default value, you must assign the int value () default 18 ;}
Import java. lang. reflect. method; public class ParseAnnoction {public static void main (String [] args) throws Exception {Class clazz = Person. class; Decription de = (Decription) clazz. getAnnotation (Decription. class); // obtain the class annotation information System. out. println (de. desc () + de. author () + de. value (); Method [] MS = clazz. getMethods (); for (Method m: MS) {Decription d = m. getAnnotation (Decription. class); if (null! = D) {// obtain the method annotation information System. out. println (d. author () + d. desc () + d. value ());}}}}
@Decription(author=cao, desc = hello annocation )public class Person {private String name;private int age;@Decription(desc = this is getName method)public String getName(){return this.name;}public int getAge(){return age;}}

 

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.