• 【记录】利用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;
        }
  • 相关阅读:
    A1015. Reversible Primes (20)
    A1024. Palindromic Number (25)
    A1023. Have Fun with Numbers (20)
    B1017. A除以B (20)
    A1059. Prime Factors (25)
    阵列命令
    工具面板
    圆角与镜像命令
    拉伸与缩放命令
    autium designer
  • 原文地址:https://www.cnblogs.com/wbl001/p/13334905.html
Copyright © 2020-2023  润新知