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