获取注解工具类
查询Config类上是否有ComponentScan注解
ComponentScan componentScan = AnnotationUtils.findAnnotation(Config.class, ComponentScan.class);
加载classpath下的类
String path = "classpath*:" + p.replace(".", "/") + "/**/*.class";
CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(path);
判断是否有某个注解
MergedAnnotations annotations = MergedAnnotations.from(method);
if (annotations.isPresent(Transactional.class)) {
return true;
}
MergedAnnotations annotations = MergedAnnotations.from(targetClass,MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
if (annotations.isPresent(Transactional.class)) {
return true;
}
获取类上的泛型
class A<T>{}
class B extends A<Teacher>{}
Type genericSuperclassType = B.class.getGenericSuperclass()
if(genericSuperclassType instanceof ParameterizedType){
((ParameterizedType) genericSuperclassType).getActualTypeArguments()[0];
}
Class clazz = GenericTypeResolver.resolveTypeArgument(B.class, A.class);
System.out.println(clazz);
查找注解
ComponentScan scan = AnnotationUtils.findAnnocation(Config.class, ComponentScan.class);
MethodParameter returnType;
returnType.getContainingClass();
读取MATA-INFO文件夹下spring.factories
List<String> names = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, classLoader);