The use of the "Java" Custom annotations-Tutorial Detailed

Source: Internet
Author: User
Annotations

When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.
To define the annotation format:
Public @interface annotation name {definition body}
Supported data types for annotation parameters:
1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. Arrays of all types above

Directly on the code:

Import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; /** * Define an annotation */@Target (Elementtype.method)//This is a comment on a method, or it can be a package, class, variable, and so many things @retention (retentionpolicy.runtime)//retention time, The general note is for the framework development in place of the configuration file use, the JVM runtime with reflection parameters processing, so generally for the runtime type @documented//used to describe other types of annotation should be labeled as the program members of the public API, It can therefore be documented publicly @interface Oneannotation {///define annotation parameters, type can be basic type and string, class, enum, array, etc., for example Javadoc this class. The default value is "String Parameter1 ()", "int parameter2 () default-1;

Classes that use annotations

/** * A class that uses custom annotations */public class Oneclass {@OneAnnotation (parameter1= "YES", parameter2=10000) public void Onemethod () { }

extracting annotation parameters

Import Java.lang.reflect.Method;  public class Testthis {public static void main (string[] args) throws Exception {//extract to the annotated method, where the reflective knowledge method is used Metho D = Class.forName ("Oneclass"). Getdeclaredmethod ("Onemethod");//The annotation we set is obtained from the method method getannotation Oneannotation = Method.getannotation (Oneannotation.class);//The two parameters that are annotated System.out.println (Oneannotation.parameter1 ( )); System.out.println (Oneannotation.parameter2 ());}

Test results:

YES

10000

Related articles:

Java Custom Annotations

Java Annotations Tutorials and custom annotations

Related videos:

Full parsing of Java annotations

Related Article

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.