zoukankan      html  css  js  c++  java
  • 【记录】利用java反射技术扫描包中包含某注解 类集合

    public static List<Class> scanClass(String classPath, Class<? extends Annotation> annotation) {
            List<Class> classList = new ArrayList<>();
            if (ObjectUtils.isEmpty(classPath)) {
                return classList;
            }
            ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
            TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true;
            provider.addIncludeFilter(includeFilter);
            Set<BeanDefinition> beanDefinitionSet = new HashSet<>();
            // 指定扫描的包名
            Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(classPath);
            beanDefinitionSet.addAll(candidateComponents);
    
            beanDefinitionSet.forEach(beanDefinition -> {
                GenericBeanDefinition definition = (GenericBeanDefinition) beanDefinition;
                try {
                    Class clazz = Class.forName(beanDefinition.getBeanClassName());
    
                    if (!ObjectUtils.isEmpty(annotation)) {
                        if (!ObjectUtils.isEmpty(AnnotationUtils.getAnnotation(clazz, annotation))) {
                            classList.add(clazz);
                        }
                    } else {
                        classList.add(clazz);
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
    //            System.out.println(definition.getBeanClassName());
            });
            return classList;
        }
  • 相关阅读:
    高精度模板_C++
    NOIP总结
    HDU2063_过山车_C++
    手写堆_C++
    NOIP2013Day1解题报告
    [ CodeVS冲杯之路 ] P1368
    POJ1002_487-3279_C++
    [ CodeVS冲杯之路 ] P1092
    POJ2376_Cleaning Shifts_C++
    欧几里得距离_曼哈顿距离_切比雪夫距离
  • 原文地址:https://www.cnblogs.com/wbl001/p/13334905.html
Copyright © 2011-2022 走看看