博客
关于我
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/

你可能感兴趣的文章
Python-如何使用FastAPI和uvicorn.run而不阻塞线程?
查看>>
Python-字典
查看>>
python-字符串
查看>>
Python-将二进制遮罩转换为多边形
查看>>
python-文件操作
查看>>
PYTHON-模块 sys os random shutil-练习
查看>>
Python-模块与包的管理
查看>>
PYTHON-模块定义 搜索路径
查看>>
Python-爬虫工程师-面试总结
查看>>
Python-类与文件读取结合
查看>>
python-类思想-实现简单增删查改
查看>>
python-网络编程
查看>>
python写入文件出现空行
查看>>
PYTHON-返回给定x的多边形路径的y坐标
查看>>
python-进程池与线程池,协程
查看>>
Python-通过函数传递变量
查看>>
Python内置函数详解——总结篇
查看>>
Python.对象集合中的标识.和散列
查看>>
Python.硒.拖放错误“AttributeError:move_to 需要 WebElement“
查看>>
Python内置函数详解
查看>>