Introduction to Annotations:
annotation annotation is the new function of jdk1.5, in today's daily development, almost inseparable from the annotations, write a short essay, to do a supplements.
Annotation effect:
The role of Annotation (annotations) is to modify packages, classes, construction methods, methods, member variables, and so on.
annotation syntax and definition form:
@interface keyword Definitions
Annotations contain members, and members are declared in the form of a parameterless method. Its method name and return value define the name and type of the member.
Member assignments are in the form of @annotation (Name=value)
Annotations need to indicate the life cycle of annotations, the adornment target of annotations, and so on, which is realized through meta annotations.
Example:
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface Target { /** * Returns An array of the kinds of element s an annotation type * can is applied to. @return An array of the kinds of elements a annotation type * can be applied to*/ Ele Menttype[] Value ();}
Meta annotation @retention, the value of member value is Retentionpolicy.runtime,
Meta annotation @target, member value is a number group, and is assigned a value of {}, with a value of Elementtype.annotation_type
Member name is value, type elementtype[]
Note classification: Built-in standard annotations:
@Override: Methods used to decorate this method that override the parent class
@Deprecated: Used to modify methods that are obsolete
@SuppressWarnnings: Used to notify the Java compiler against specific compilation warnings
Meta-Annotations:
@Target: Description of the range of objects modified by annotation
@Retention: Describe the life cycle of annotation
@Documented: Documented is a markup annotation, primarily for Javadoc
@Inherited: Inherited is also a markup annotation, and if a inherited annotation is decorated with a class, it inherits from the subclass of the class and inherits the annotation as well.
This completes the inheritance, but the annotations themselves are not allowed to inherit.
General notes:
Custom annotations to accomplish a specific task
Annotation life cycle:
The life cycle of annotations is modified by the meta-annotations retention
Look at the source of the retention
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) Public@InterfaceRetention {/*** Returns the retention policy.*@returnThe retention policy*/retentionpolicy value ();} Public enumRetentionpolicy {/*** Annotations is to being discarded by the compiler.*/SOURCE,/*** Annotations is to being recorded in the class file by the compiler* and need not being retained by the VM at run time. This is the default* behavior.*/CLASS,/*** Annotations is to being recorded in the class file by the compiler and* retained by the VM at run time, so they may Read reflectively.**@seejava.lang.reflect.AnnotatedElement*/RUNTIME}
Source: Annotations remain in source files, annotations are discarded when Java files are compiled into class files
Class: Annotations are persisted to the class file, and the JVM is discarded when it loads the class file. This is the default life cycle
RUNTIME: Annotations are not only saved to the class file, but are still present after the JVM loads the class file, saved to a class object, and can be retrieved by reflection
Adornment target for annotations:
The adornment target of the annotation is the object modified by the meta-annotation target.
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) Public@InterfaceTarget {/*** Returns An array of the kinds of elements a annotation type* can be applied to.*@returnAn array of the kinds of elements an annotation type* can is applied to*/elementtype[] Value ();} Public enumElementType {/**Class, interface (including annotation type), or enum declaration*/TYPE,/**Field declaration (includes enum constants)*/FIELD,/**Method Declaration*/METHOD,/**formal parameter declaration*/PARAMETER,/**Constructor Declaration*/CONSTRUCTOR,/**Local Variable Declaration*/local_variable,/**Annotation Type declaration*/Annotation_type,/**Package Declaration*/Package ,/*** Type parameter declaration**@since1.8*/Type_parameter,/*** Use of a type**@since1.8*/Type_use}
Type: Refers to annotations that are used on classes, interfaces (including annotations), or enums
Field: Refers to the annotation used in the field property, also including Enum constants
Method: Refers to annotations that are used on methods declarations
PARAMETER: Refers to annotations that are used on parameters
CONSTRUCTOR: Refers to annotations that are used in the constructor
Local_variable: Refers to annotations that are used on local variables
Annotation_type: Refers to the meta-annotations used on annotations
Package: Refers to annotations that are used on packages
Custom annotations:
Custom annotations are an important aspect of using annotations, and this time we analyze a custom annotation in the project
The code is as follows:
@Target (Elementtype.method) @Retention (retentionpolicy.runtime) Public@InterfaceLogmethod {} @Slf4j Public classLogmethodinterceptorImplementsMethodinterceptor {@Override PublicObject Invoke (Methodinvocation methodinvocation)throwsthrowable {String className=string.valueof (Methodinvocation.getmethod (). Getdeclaringclass ()); String MethodName=Methodinvocation.getmethod (). GetName (); object[] Arguments=methodinvocation.getarguments (); Log.info ("Before:" + ClassName + "." + MethodName + "()"); Log.info ("Arguments:" +jsontools.tojsonstring (arguments));LongStart =System.currenttimemillis (); Object resultobj=methodinvocation.proceed ();LongEnd =System.currenttimemillis (); Log.info ("After:result is" + jsontools.tojsonstring (resultobj) + ", the Execute Time is" + (End-start) + "MS");returnresultobj;}}
Specific configuration:
<BeanID= "Logmethodinterceptor"class= "Com.*logmethodinterceptor"/><Aop:configProxy-target-class= "true"><Aop:pointcutID= "Logmethodpointcut"expression= "Execution (* com*service.impl.*.* (..)) and @annotation (com.*annotation. Logmethod) "/><Aop:advisorAdvice-ref= "Logmethodinterceptor"Pointcut-ref= "Logmethodpointcut"/></Aop:config>
Logmethod defines an annotation that has a life cycle effect on method
Logmethodinterceptor Annotations for processors
Use spring's AOP to print the relevant log of the parameters separately in the method's entry and exit
Java Annotations Supplements