Java Custom Annotations

Source: Internet
Author: User

In spring applications, annotations are often used to develop, which facilitates faster development.

Describe the custom annotations:

First, custom annotations create a new @interface, which is an annotated interface that has several annotations on this interface:

      @Documented markup Generation Javadoc

@Retention the lifetime of the annotations, the values are:

Take value

Describe

SOURCE

Valid in the source file (that is, the source file is reserved, and the boxed box is picked).

CLASS

Valid in class file (that is, class is reserved, and the boxed box is picked).

RUNTIME

Valid at run time (that is, run-time retention) 

@Target the target of the label, the value is

CONSTRUCTOR

Used to describe the constructor (lead Bento).

FIELD

Used to describe the domain (lead bento).

Local_variable

Used to describe local variables (pick-up bento).

METHOD

Used to describe a method.

Package

Used to describe the package (pick box).

PARAMETER

Used to describe parameters.

TYPE

Used to describe a class or interface (even an enum).

@Inherited Tag Inheritance Relationships

Example: Create an annotation that acts on a class @person

1. First create an annotation class person

        

 Packagecom.wuyu.annotation;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target, @Documented @target (elementtype.type) @Retention (retentionpolicy.runtime) Public@InterfacePerson {String name ()default""; intAge ()default0;}

2. Create a test class

 Packagecom.wuyu.annotation; @Person (name= "Test") Public classTest { Public Static voidMain (string[] args) {getpersion (Test.class); }     Public Static voidGetpersion (class<?>c) {        if(C.isannotationpresent (person).class) { person person= C.getannotation (person.class); if(Person! =NULL) {System.out.println ("Name=" + person.name () + ", age=" +person.age ()); }        }    }}

3. Implementation results:

Name=test,age=0

Java Custom 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.