作用

按照我的理解AnnotationAttributes是为了存储注解属性或者信息的,可以通过org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes(annotation);得到
比如:

@ComponentScan
public class Math {public static void main(String[] args) {Annotation annotation= Math.class.getAnnotation(ComponentScan.class);System.out.println("annotationType=>"+annotation.annotationType());AnnotationAttributes ComponentScanAnnotationAttributes= (AnnotationAttributes) AnnotationUtils.getAnnotationAttributes(annotation);ComponentScanAnnotationAttributes.forEach((key,value) ->{System.out.println(key+"=>"+value);});//如果不强转,就直接得到了MapMap<String,Object> map=AnnotationUtils.getAnnotationAttributes(annotation);Set<String> set=map.keySet();for(String key:set){System.out.println(key+"=>"+map.get(key));}}
}
输出如下
annotationType=>interface org.springframework.context.annotation.ComponentScan
basePackageClasses=>[Ljava.lang.Class;@1563da5
basePackages=>[Ljava.lang.String;@2bbf4b8b
excludeFilters=>[Lorg.springframework.context.annotation.ComponentScan$Filter;@30a3107a
includeFilters=>[Lorg.springframework.context.annotation.ComponentScan$Filter;@33c7e1bb
lazyInit=>false
nameGenerator=>interface org.springframework.beans.factory.support.BeanNameGenerator
resourcePattern=>**/*.class
scopeResolver=>class org.springframework.context.annotation.AnnotationScopeMetadataResolver
scopedProxy=>DEFAULT
useDefaultFilters=>true
value=>[Ljava.lang.String;@2bbf4b8b
SimpleName=>BeanNameGenerator

当然也可以获取注解的注解

构造函数以及成员

public class AnnotationAttributes extends LinkedHashMap<String, Object> {private static final String UNKNOWN = "unknown";//表示注解类型@Nullableprivate final Class<? extends Annotation> annotationType;//未知final String displayName;boolean validated = false;public AnnotationAttributes() {this.annotationType = null;this.displayName = UNKNOWN;}/*AnnotationAttributes 继承了LinkedHashMap,所以这个构造方法通过initialCapacity初始化指定容量的空间*/public AnnotationAttributes(int initialCapacity) {super(initialCapacity);this.annotationType = null;this.displayName = UNKNOWN;}public AnnotationAttributes(Map<String, Object> map) {super(map);this.annotationType = null;this.displayName = UNKNOWN;}public AnnotationAttributes(AnnotationAttributes other) {super(other);this.annotationType = other.annotationType;this.displayName = other.displayName;this.validated = other.validated;}public AnnotationAttributes(Class<? extends Annotation> annotationType) {Assert.notNull(annotationType, "'annotationType' must not be null");this.annotationType = annotationType;this.displayName = annotationType.getName();}AnnotationAttributes(Class<? extends Annotation> annotationType, boolean validated) {Assert.notNull(annotationType, "'annotationType' must not be null");this.annotationType = annotationType;this.displayName = annotationType.getName();this.validated = validated;}public AnnotationAttributes(String annotationType, @Nullable ClassLoader classLoader)               {Assert.notNull(annotationType, "'annotationType' must not be null");this.annotationType = getAnnotationType(annotationType, classLoader);this.displayName = annotationType;}
}

可以看到,AnnotationAttributes 继承了LinkedHashMap,我们可以像遍历map那样遍历它,如文章开头代码。

主要方法

 /**得到注解类型* @since 4.2*/@Nullablepublic Class<? extends Annotation> annotationType() {return this.annotationType;}/**得到类型为String的注解内部属性值。*/public String getString(String attributeName) {return getRequiredAttribute(attributeName, String.class);}/**得到类型为String数组的注解内部属性值。*/public String[] getStringArray(String attributeName) {return getRequiredAttribute(attributeName, String[].class);}/**得到类型为Boolean的注解内部属性值。*/public boolean getBoolean(String attributeName) {return getRequiredAttribute(attributeName, Boolean.class);}/**得到类型为Number的注解内部属性值*/@SuppressWarnings("unchecked")public <N extends Number> N getNumber(String attributeName) {return (N) getRequiredAttribute(attributeName, Number.class);}/**得到类型为Enum的注解内部属性值*/@SuppressWarnings("unchecked")public <E extends Enum<?>> E getEnum(String attributeName) {return (E) getRequiredAttribute(attributeName, Enum.class);}/**得到类型为Class的注解内部属性值比如在ComponentScan注解内部有一个属性为:Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;*/@SuppressWarnings("unchecked")public <T> Class<? extends T> getClass(String attributeName) {return getRequiredAttribute(attributeName, Class.class);}/**得到类型为Class数组的注解内部属性值*/public Class<?>[] getClassArray(String attributeName) {return getRequiredAttribute(attributeName, Class[].class);}/**得到类型为Annotation的注解内部属性值* @since 4.2*/public AnnotationAttributes getAnnotation(String attributeName) {return getRequiredAttribute(attributeName, AnnotationAttributes.class);}/**得到类型为Annotation的注解内部属性值,需指定注解的Class* @since 4.2*/public <A extends Annotation> A getAnnotation(String attributeName, Class<A> annotationType) {return getRequiredAttribute(attributeName, annotationType);}/**得到类型为Annotation数组的注解内部属性值*/public AnnotationAttributes[] getAnnotationArray(String attributeName) {return getRequiredAttribute(attributeName, AnnotationAttributes[].class);}/*** @since 4.2*/@SuppressWarnings("unchecked")public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class<A> annotationType) {Object array = Array.newInstance(annotationType, 0);return (A[]) getRequiredAttribute(attributeName, array.getClass());}/**将Map转化为AnnotationAttributes */@Nullablepublic static AnnotationAttributes fromMap(@Nullable Map<String, Object> map) {if (map == null) {return null;}if (map instanceof AnnotationAttributes) {return (AnnotationAttributes) map;}return new AnnotationAttributes(map);}

核心方法 getRequiredAttribute

private <T> T getRequiredAttribute(String attributeName, Class<T> expectedType) {Assert.hasText(attributeName, "'attributeName' must not be null or empty");Object value = get(attributeName);//调用上一层Map的get的方法assertAttributePresence(attributeName, value);//判断value是否为空assertNotException(attributeName, value);//判断是否抛出异常//getComponentType用于返回表示数组的组件类型的Class//只有数组类型才会有getComponentType,所以这里意思是//如果是数组类型,并且是数组类型正确,就,,,,这个暂时没有理解,如果有知道的,请在下方评论if (!expectedType.isInstance(value) && expectedType.isArray() &&expectedType.getComponentType().isInstance(value)) {Object array = Array.newInstance(expectedType.getComponentType(), 1);Array.set(array, 0, value);value = array;}assertAttributeType(attributeName, value, expectedType);//检查类型是否正确return (T) value;
}private void assertAttributePresence(String attributeName, Object attributeValue) {Assert.notNull(attributeValue, () -> String.format("Attribute '%s' not found in attributes for annotation [%s]",attributeName, this.displayName));
}private void assertNotException(String attributeName, Object attributeValue) {if (attributeValue instanceof Throwable) {throw new IllegalArgumentException(String.format("Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]",attributeName, this.displayName, attributeValue), (Throwable) attributeValue);}
}private void assertAttributeType(String attributeName, Object attributeValue, Class<?> expectedType) {if (!expectedType.isInstance(attributeValue)) {throw new IllegalArgumentException(String.format("Attribute '%s' is of type %s, but %s was expected in attributes for annotation [%s]",attributeName, attributeValue.getClass().getSimpleName(), expectedType.getSimpleName(),this.displayName));}
}

以上那些get方法都是基于getRequiredAttribute实现的。

AnnotationAttributes相关推荐

  1. java getstringarray_Java AnnotationAttributes.getStringArray方法代碼示例

    本文整理匯總了Java中org.springframework.core.annotation.AnnotationAttributes.getStringArray方法的典型用法代碼示例.如果您正苦 ...

  2. SpringBoot自动配置的原理及实现

    SpringBoot的核心就是自动配置,自动配置是基于条件判断配置Bean 自动配置的源码在spring-boot-autoconfigure-2.2.13.RELEASE SpringBoot运行原 ...

  3. Spring Boot 中 @EnableXXX 注解的驱动逻辑

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者 | 温安适 来源 | https://juejin. ...

  4. Spring Boot 中的 @EnableAutoConfiguration 是如何处理的?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者 | 温安适 来源 | https://my.oschina. ...

  5. 深入聊一聊 Spring AOP 实现机制

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 本文转载于公众号:吉姆餐厅ak 概述 AOP(Aspect-Oriented Programmin ...

  6. 神秘又强大的@SpringBootApplication注解

    作者:vivo 互联网服务器团队-Peng peng 一.前言 大部分的配置都可以用Java类+注解来代替,而在SpringBoot项目中见的最多的莫过于@SpringBootApplication注 ...

  7. 一次性搞懂Spring Boot 注解原理与自动装配原理,图文并茂,万字长文!

    原文:cnblogs.com/jing99/p/11504113.html 首先,先看SpringBoot的主配置类: @SpringBootApplication public class Star ...

  8. Spring Boot 基于注解驱动源码分析--自动配置

    Spring作为Java开发最常用的容器管理框架,使用注解为我们提供很多便捷,下面通过源码分析Spring基于注解驱动自动配置的原理 首先介绍两个关键类: ConfigurationClassPost ...

  9. 追踪解析Spring ioc启动源码(2)

    接上篇 3 reader 注册配置类 该 part 的起点: public AnnotationConfigApplicationContext(Class<?>... annotated ...

最新文章

  1. linux跨主机复制文件
  2. 3.Chrome数据同步服务分析--server一片
  3. JZOJ 3875. 【NOIP2014八校联考第4场第2试10.20】星球联盟(alliance)
  4. Scala模式匹配:对象匹配
  5. ITK:用常量填充图像
  6. Linux下C语言执行过程(预处理,编译,汇编,链接,执行)
  7. mysql 命令 _mysql 命令
  8. quadprog函数的介绍和应用,二次规划函数
  9. selenium 定位方式3-css_selector
  10. kvm虚拟机设置万兆网卡_kvm已经设置桥接网卡的虚拟机无法连接宿主机?
  11. 全面超越人类!Google称霸SQuAD,BERT横扫11大NLP测试
  12. 如何对数据目标进行分析
  13. 日志打印工具类LogUtils
  14. 多态与异常处理——动手动脑
  15. C#封装DLL类库,调用类库
  16. 01使用Python分析科比生涯数据
  17. html制作公众号,自制微信公众号一键排版工具
  18. 神经网络和深度学习二者之间的关系
  19. 戴尔笔记本linux不能开机启动,戴尔笔记本无法开机的解决方法
  20. 计算机操作系统-文件管理 知识点归纳

热门文章

  1. window.location的使用,能查看当前网址的端口等等
  2. 空间点到平面的垂足坐标的计算方法以及matlab实现
  3. eclipse启动Tomcat报错,显示8005, 8080端口被占用
  4. 解决AXmath公式编号从0开始的问题
  5. 非稳态计算时间步和最大迭代数的设定(分享)
  6. 计算机辅助设计高级绘图员技能鉴定试题,计算机辅助设计高级绘图员技能鉴定试卷.doc...
  7. 半导体测试——CP测试,WAT和Final Test终测
  8. iterm上安装oh-my-zsh连接失败
  9. 3389远程服务器管理器,server 2012R2 data center远程桌面无法连接,3389不通,监听列表没有3389...
  10. 树莓派无法连接远程计算机,如何从树莓派远程连接到Windows PC