2019-10-31

Spring注解工具类

1、AnnotationUtils

AnnotationUtils是Spring提供的重要的注解工具类,主要方法有以下几个:

public staticA getAnnotation(Annotation annotation,Class annotationType)

publicstatic A getAnnotation(AnnotatedElement annotatedElement, Class annotationType)

publicstatic A getAnnotation(Method method, Class annotationType)

public staticAnnotation[] getAnnotations(AnnotatedElement annotatedElement)

public staticAnnotation[] getAnnotations(Method method)

publicstatic Set getRepeatableAnnotations(AnnotatedElementannotatedElement,Class annotationType)

publicstatic Set getRepeatableAnnotations(AnnotatedElementannotatedElement,Class annotationType,@Nullable ClasscontainerAnnotationType)

publicstatic Set getDeclaredRepeatableAnnotations(AnnotatedElementannotatedElement,Class annotationType)

publicstatic A findAnnotation(AnnotatedElement annotatedElement,Class annotationType)

publicstatic A findAnnotation(Method method,@Nullable Class annotationType)

publicstatic A findAnnotation(Class clazz, Class annotationType)

public staticClass findAnnotationDeclaringClass(Class annotationType,@Nullable Class clazz)

public staticClass findAnnotationDeclaringClassForTypes(List> annotationTypes,@Nullable Class clazz)

public staticboolean isAnnotationInherited(Class annotationType, Class clazz)

public staticboolean isAnnotationMetaPresent(ClassannotationType,@Nullable Class metaAnnotationType)

public staticObject getDefaultValue(Annotation annotation)

public staticObject getDefaultValue(@Nullable Annotation annotation,@Nullable StringattributeName)

其中,getAnnotation方法,返回的是一个经过spring动态代理后的Annotation,通过getAnnotation方法不仅可以返回当前传入的注解,还可以获取注解的元注解

例如:

//自定义注解类MyAnnotation

@Target({ElementType.TYPE})

@Service

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public @interfaceMyAnnotation {}

//自定义注解类MyMethodAnnotation 

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface MyMethodAnnotation {}


@MyAnnotation

public class AnnotationModel {

    @MyMethodAnnotation

    public void call(String name){

        System.out.println("call:"+name);

    }

}

public void testAnnotationUtils() throws NoSuchMethodException {

    MyAnnotation myAnn = AnnotationModel.class.getAnnotation(MyAnnotation.class);

    //获取类上的注解

    Annotation ann = AnnotationUtils.getAnnotation(myAnn,MyAnnotation.class);

    System.out.println(ann);  //@com.xc.annotate.MyAnnotation()

    /***获取元注解Service*/

    Service service1 = AnnotationUtils.getAnnotation(MyAnnotation.class,Service.class);

    System.out.println(service1); //@org.springframework.stereotype.Service(value=)

    /***获取元注解Retention*/

    Retention retention = AnnotationUtils.getAnnotation(myAnn,Retention.class);

    System.out.println(retention); //@java.lang.annotation.Retention(value=RUNTIME)

    //获取方法上的注解

    Method method = AnnotationModel.class.getDeclaredMethod("call", String.class);

    Annotation annotation = AnnotationUtils.getAnnotation(method, MyMethodAnnotation.class);

    System.out.println(annotation);  //@com.xc.annotate.MyMethodAnnotation()

    //获取方法上的所有注解

    Annotation[] annotations = AnnotationUtils.getAnnotations(method);

    for(Annotation a:annotations){

        System.out.println(a);

//@com.xc.annotate.MyMethodAnnotation()@org.springframework.web.bind.annotation.RequestMapping(name=, value=[], path=[], method=[], params=[], headers=[], produces=[], consumes=[])

   }

}







AnnotatedElementUtils注释元素工具类,主要用于查找注释,元注释

public classAnnotateUtils {

/**

    *判断一个类是否含有指定的注解

*@param clsType

*@param annotationClass

*@return

*/

publicstatic boolean hasAnnotation(Class clsType, Class annotationClass){

returnAnnotatedElementUtils.hasAnnotation(clsType,annotationClass);

}

}

你可能感兴趣的:(2019-10-31)