美文网首页
2019-10-31

2019-10-31

作者: clicsug | 来源:发表于2019-10-31 10:23 被阅读0次

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 竹枝词-户部巷过早二首 陈武林 2019-10-31 (一) 古来户部隐江城,小巷悠然...

  • lxy的ScalersTalk第五轮新概念朗读持续力训练Day8

    [Day 1861 2019-10-31] Lesson21 Mad ornot? Aeroplanes are ...

  • 反射

    2019-10-31 // getName():获得类的完整名字。 // getgetSimpleName...

  • IO

    2019-10-31 package com.lixing.io11; import java.io.*; pub...

  • 10月31日

    2019-10-31 毛雅亭 字数 480 · 阅读 22 2019-09-25 08:27 ...

  • 读书笔记:了不起的我

    名称:《了不起的我 —— 自我发展的心理学》 阅读时间:2019-10-29 - 2019-10-31 (3天) ...

  • 文先森的日常 -- 剩46天

    日精进打卡第446天 姓名:李文杰 (四爷); 公司:中国太平人寿; 日期:2019-10-31 【知~学习】 《...

  • 常见类型 范围大小

    2019-10-31 顺便记忆下 个、十、百、千、万、十万、百万、千万、亿、兆(10的12次方)、京〔10的16次...

  • 2019-10-31

    2019-10-31 姓名:符振华(379期 反省三组) 公司:深圳蔚蓝时代商业管理有限公司-上海第一分公司 【日...

  • 鉴峰丨平凡,不要平淡

    鉴峰自我管理 [连续签到第659天] 2019-10-31 周四 生活有喜怒哀乐, 也有柴米油盐, 平凡, 我也是...

网友评论

      本文标题:2019-10-31

      本文链接:https://www.haomeiwen.com/subject/wpmzvctx.html