1. 程式人生 > >spring中自定義註解(annotation)與獲取註解

spring中自定義註解(annotation)與獲取註解

註解類自定義

package me.lichunlong.spring.annotation;


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;   

@Target({ElementType.METHOD})   
@Retention(RetentionPolicy.RUNTIME)   
@Documented

public @interface LogAnnotation {   
    String desc() default "無描述資訊";   

}  

獲取方法及所在類上的某個註解

public static Object[] findAnnotation(Method method, Class annotationClass){
    Object methodAnnotation = method.getAnnotation(annotationClass);
    Object classAnnotation = method.getDeclaringClass().getAnnotation(annotationClass);
    Object[] result = {classAnnotation, methodAnnotation
}; return result; }