今天主要说一下,大家在研读或者使用spring的过程中,可能忽略一些了spring提供的内部工具类,而且这些内部工具对于我们开发人员使用起来是非常的爽,效率也高,所以今天就简单介绍几个.
1 AnnotationUtils:处理注解的工具类
介绍Class提供的获取注解相关方法:
1.1 <A extends Annotation>A getAnnotation(Class<A>annotationClass):获取该class对象对应类上指定类型的Annotation,如果该类型注解不存在,则返回null
1.2 Annotation[] getAnnotations():返回修饰该class对象对应类上存在的所有Annotation
1.3 <A extends Annotation>A getDeclaredAnnotation(Class<A>annotationClass):这是Java 8中新增的,该方法获取直接修饰该class对象对应类的指定类型的Annotation,如果不存在,则返回null(也就说只找自己的,继承过来的注解这个方法就不管了)
1.4 Annotation[] getDeclaredAnnotations():返回修饰该Class对象对应类上存在的所有Annotation(同上,继承的不管)
1.5 <A extends Annotation>A[] getAnnotationByType(Class<A>annotationClass):该方法的功能与前面介绍的getAnnotation()方法基本相似,但由于Java8增加了重复注解功能,因此需要使用该方法获取修饰该类的指定类型的多个Annotation(会考虑继承的注解)
1.6 <A extends Annotation>A[] getDeclaredAnnotationByType(Class<A>annotationClass):
2 AnnotatedElementUtils:查找注解内部具体的详细信息
2.2 public static AnnotatedElement forAnnotations(final Annotation... annotations):给这么多的Annos提供一个适配器(内部就是new了一个AnnotatedElement匿名内部类,没啥特殊的)
2.2 public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, String annotationName):简单的说,就是返回指定Class上面这个注解上的注解(若没有,返回null) 备注:不包含Java的元注解哦~
2.3 public static boolean hasMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType):
2.4 public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType)
3 AnnotationBeanUtils:拷贝注解值到指定的Bean中
3 AnnotationConfigUtils:和Config配置类有关的注解工具类
3.1 public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(BeanDefinitionRegistry registry, @Nullable Object source):该方法主要是向容器注册了一组基础设施PostProcessor bean定义,这些bean定义生成的PostProcessor实例被框架自己用于识别注解配置类中的bean定义(就是我们上面说的7大默认Bean定义,role均为:BeanDefinition.ROLE_INFRASTRUCTURE表示框架自己用的)。 它还为Bean工厂设置了:setDependencyComparator和setAutowireCandidateResolver
3.2 public static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd):处理通用的Bean定义上的注解,该方法从原始bean定义的元数据中获取那些通用的注解信息:@Lazy,@DependsOn,@Role,@Description,然后设置AnnotatedBeanDefinition实例相应的属性
4 ConfigurationClassUtils
:Spring内部处理@Configuration
的工具类
5 AnnotationAttributes:Map的封装
5.1 AnnotatedTypeMetadata#getAnnotationAttributes:获取注解所有的属性
5.2 AnnotationAttributes#fromMap :转化成map结构
5.3 AnnotatedElementUtils#getMergedAnnotationAttributes等系列方法