17th Article-java Annotation annotations

Source: Internet
Author: User

17th Article-java Annotation annotations
    • every sentence : After really hard work you will find yourself much better than you think.

    • Beginner's experience : With a struggling heart, all hardships and hardships from the time to solve

    • ( Author: jeep/711) [Java Notes | time: 2017-05-17| JAVA annotation Annotations]



1. What is an annotation (Annotation)

Annotation is actually a special tag in the code, it is used to replace the configuration file, that is, the traditional way through the configuration file tells the class how to run, with the annotation technology, the developer can tell the class how to run through annotations. The typical application of annotations in Java technology is that the annotations within the class can be obtained through the reflection technique to determine how to run the class

  • Annotation (note) Is that Java provides a way and method for associating any information and any metadata (metadata) with elements in a meta-program.

  • Annotion (annotation) is an interface that a program can use to get the Annotion object of a specified program element by reflection, and then obtain metadata from the annotation by Annotion object

  • Annotation (note) was introduced in JDK5.0 and later versions. It can be used to create documents, track dependencies in code, and even perform basic compile-time checks. In some ways, annotation is used as a modifier and applied to the declaration of a package, type, constructor method, method, member variable, parameter, local variable

2. Basic annotation
1.[email protected]: qualifies overriding the parent class method, which can only be used for methods [email protected]: used to indicate that a program element (class, method, and so on) is obsolete [email protected]: suppress compiler warnings
3. Custom annotation
  • defining annotation with @interface

  • Using annotation modifiers such as classes, methods, variables, interfaces, and so on, we usually put annotation before all modifiers

  • Defining a annotation with member variables

  • Specifying an initial value for a member variable of annotation

4. Extracting annotation Information
  • The annotation interface represents the comment that precedes the program element, which is the parent interface for all annotation types

  • Annotatedelement interface represents a program element in a program that can accept annotations

  • Call the Annotatedelement object with the following three methods to access the annotation information:

  • Getannotation (Class Annotationclass): Returns a comment of the specified type that exists on the program element, or null if the comment for that type does not exist

  • Annotation[] Getannotations (): Returns all comments that exist on the program element

  • Boolean isannotationpresent (class<? extends Annotation> Annotationclass): Determines whether the program element contains a comment of the specified type, which, if present, returns True. otherwise returns false

5.Annotation Annotation Instances

Custom annotation

1.import java.lang.annotation.documented;  2.import java.lang.annotation.elementtype;   3.import java.lang.annotation.Retention;  4.import  java.lang.annotation.retentionpolicy;  5.import java.lang.annotation.target;  6./**  7. *  define an annotation  8. */  [email protected] (elementtype.method)  //   This is an annotation of the method, it can also be a package, class, variable, and so many things   [email protected] (retentionpolicy.runtime)  //  retention time, The general note is for the framework development in place of the configuration file, the JVM runtime with reflection parameters processing, so it is generally runtime type   [email protected] //  Used to describe other types of annotation that should be used as public APIs for annotated program members, so that they can be documented by tools such as Javadoc   12.public  @interface   oneannotation {  13.    //  defines parameters for annotations, type can be basic type and string, Class, enum, array, etc. Default defaults to   14.    string parameter1 ()  default  "";   15.    int parameter2 ()  default -1;   16.} 

Using annotations

1./** 2. * A Class 3 that uses custom annotations.    */4.public class Oclass {5.    @OneAnnotation (parameter1= "YES", parameter2=10) 6.    public void Onemethod () {7. } 8.}

get annotation parameters

1.import java.lang.reflect.method;  2.public class testdemo {  3.     public static void main (String[] args)  throws Exception  {  4.        //  extract to the annotated method, where the knowledge of reflection is used    5.        method method = class.forname ("OneClass "). Getdeclaredmethod (" Onemethod ");  6.        //  From method method Getannotation Get our set annotations   7.         Oneannotation oneannotation = method.getannotation (Oneannotation.class);  8.         //  get annotations for both parameters   9.         system.out.println (Oneannotation.parameter1 ());  10.         system.out.println (OneannotaTion.parameter2 ());   11.    }  12.} 

Beginner (JAVA annotation annotations advanced stage) Difficulty: ★★★★★ hope that each article can provide readers with help and promotion, which is the original intention of every author Thank you for your reading welcome your message and suggestions
    • Facebook:jeep SevenEleven

    • Twitter: @JEEP7ll

    • Sina official Weibo: @JEEP-711

    • GitHub Blog: Https://github.com/jeep711/jeep711.github.io

    • Blog garden:http://www.cnblogs.com/jeep711/

    • w3c/blog:http://www.w3cschool.cn/jeep711blog/

    • csdn/blog:http://blog.csdn.net/jeep911

    • 51cto/blog:http://jeep711.blog.51cto.com/

    • Code Cloud: Http://git.oschina.net/JEEP711/jeep711.github.io

    • Email: [email protected],[email protected]



This article is from the "JEEP711" blog, make sure to keep this source http://jeep711.blog.51cto.com/12970787/1955820

17th-java Annotation Annotations

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.