spring中自定义注解(annotation)与获取注解

时间:2022-12-22 20:40:33

注解类自定义

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;
}