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

你可能感兴趣的文章
Python3 tutorial day3
查看>>
python3 week4
查看>>
Python3 创建数组
查看>>
python3 套接字异常(a byte-like object is required not str)
查看>>
python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
查看>>
python3 文本变图片
查看>>
Python3 查看 com 组件的属性
查看>>
python3 案例分享--发邮件
查看>>
python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
查看>>
Python3 求最大/小值及索引值 Numpy
查看>>
python3+django使用celery执行某些任务失败的解决方案
查看>>
Python3+Requests+Unittest实战系列【一】
查看>>
python3+requests+unittest实战系列【一】
查看>>
python3+requests+unittest实战系列【二】
查看>>
Python3+Requests+Unittest实战系列【二】
查看>>
python3----ASCII
查看>>
Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
查看>>
Python3-如何自我更新作为服务运行的应用程序
查看>>
Python3.1-网格模拟概念性问题
查看>>
Python3.12 解决conda环境libstdc++版本过低问题
查看>>