Java annotation mechanism is widely used in various frameworks, and some values can be set in annotations.
To get the annotation value set in the annotation class annotation: traverse the method in the custom annotation, reflect the execution method, and the result is the corresponding annotation value.
Java code example:
Package Com.doctor.spring.core;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;import java.lang.reflect.method;/** * Custom annotation to get the annotation value set in the annotation class annotation That is: Iterate through the methods in the custom annotation, reflect the execution method, and the result is the corresponding annotation value. * * For complex annotation processing see: {@code annotationutils} * * @author doctor * * @time January 23, 2015 PM 6:15:31 */public Class Annotationpract Ice {public static void main (string[] args) throws Reflectiveoperationexception {myannotation myannotation = Annotationp. Class.getannotation (Myannotation.class); System.out.println (myannotation);//output://@com. Doctor.spring.core.annotationpractice$myannotation (value= annotationp,//num=12, address=[1, 2]) for (Method Method:myAnnotation.annotationType (). Getdeclaredmethods ()) {if (! Method.isaccessible ()) {method.setaccessible (true);} Object invoke = Method.invoke (myannotation); System.out.println ("Invoke METHD" + method.getname ()+ "Result:" + Invoke); if (Invoke.getclass (). IsArray ()) {object[] temp = (object[]) invoke;for (Object o:temp) {SYSTEM.O Ut.println (o);}} Output://Invoke METHD num result:12//invoke METHD value result:annotationp//invoke METHD address Result:[ljava.lang.stri ng;@74a14482//1//2} @Retention (Retentionpolicy.runtime) @Target (elementtype.type) @Documentedprivate @interface myannotation {String value () default ""; int num () default 100; String[] Address () default {};} @MyAnnotation (value = "Annotationp", num =, address = {"1", "2"}) private static class Annotationp {}}
Java custom annotation to get annotation values annotation set in the annotation class