博客
关于我
Spring根据包名获取包路径下的所有类
阅读量:428 次
发布时间:2019-03-06

本文共 4280 字,大约阅读时间需要 14 分钟。

import org.apache.commons.lang3.ArrayUtils;import org.springframework.beans.factory.BeanDefinitionStoreException;import org.springframework.context.ResourceLoaderAware;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternUtils;import org.springframework.core.type.classreading.CachingMetadataReaderFactory;import org.springframework.core.type.classreading.MetadataReader;import org.springframework.core.type.classreading.MetadataReaderFactory;import org.springframework.core.type.filter.AnnotationTypeFilter;import org.springframework.core.type.filter.TypeFilter;import org.springframework.util.StringUtils;import org.springframework.util.SystemPropertyUtils;import java.io.IOException;import java.lang.annotation.Annotation;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Set;public class ClassScaner implements ResourceLoaderAware {    private final List includeFilters = new LinkedList<>();    private final List excludeFilters = new LinkedList<>();    private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();    private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);    public static Set> scan(String[] basePackages, Class... annotations) {        ClassScaner cs = new ClassScaner();        if (ArrayUtils.isNotEmpty(annotations)) {            for (Class anno : annotations) {                cs.addIncludeFilter(new AnnotationTypeFilter(anno));            }        }        Set> classes = new HashSet<>();        for (String s : basePackages) {            classes.addAll(cs.doScan(s));        }        return classes;    }    public static Set> scan(String basePackage, Class... annotations) {        return ClassScaner.scan(StringUtils.tokenizeToStringArray(basePackage, ",; \t\n"), annotations);    }    public final ResourceLoader getResourceLoader() {        return this.resourcePatternResolver;    }    public void setResourceLoader(ResourceLoader resourceLoader) {        this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);        this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);    }    public void addIncludeFilter(TypeFilter includeFilter) {        this.includeFilters.add(includeFilter);    }    public void addExcludeFilter(TypeFilter excludeFilter) {        this.excludeFilters.add(0, excludeFilter);    }    public void resetFilters(boolean useDefaultFilters) {        this.includeFilters.clear();        this.excludeFilters.clear();    }    public Set> doScan(String basePackage) { Set> classes = new HashSet<>(); try { String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage)) + "/**/*.class"; Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath); for (int i = 0; i < resources.length; i++) { Resource resource = resources[i]; if (resource.isReadable()) { MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource); if ((this.includeFilters.size() == 0 && this.excludeFilters.size() == 0) || this.matches(metadataReader)) { try { classes.add(Class.forName(metadataReader.getClassMetadata().getClassName())); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } } catch (IOException ex) { throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex); } return classes; } protected boolean matches(MetadataReader metadataReader) throws IOException { for (TypeFilter tf : this.excludeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { return false; } } for (TypeFilter tf : this.includeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { return true; } } return false; } public static void main(String[] args) { ClassScaner.scan("com.hjzgg.test.thirdjar.config,com.hjzgg.test.thirdjar.web", null).forEach(clazz -> System.out.println(clazz)); }}

转载地址:http://gieuz.baihongyu.com/

你可能感兴趣的文章
numpy绘制热力图
查看>>
numpy转PIL 报错TypeError: Cannot handle this data type
查看>>
Numpy闯关100题,我闯了95关,你呢?
查看>>
Nutch + solr 这个配合不错哦
查看>>
NuttX 构建系统
查看>>
NutUI:京东风格的轻量级 Vue 组件库
查看>>
NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
查看>>
NutzWk 5.1.5 发布,Java 微服务分布式开发框架
查看>>
NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
查看>>
Nuxt Time 使用指南
查看>>
NuxtJS 接口转发详解:Nitro 的用法与注意事项
查看>>
NVelocity标签使用详解
查看>>
NVelocity标签设置缓存的解决方案
查看>>
Nvidia Cudatoolkit 与 Conda Cudatoolkit
查看>>
NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
查看>>
NVIDIA-cuda-cudnn下载地址
查看>>
nvidia-htop 使用教程
查看>>
nvidia-smi 参数详解
查看>>
Nvidia驱动失效,采用官方的方法重装更快
查看>>
nvmw安装node-v4.0.0之后版本的临时解决办法
查看>>