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

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

参考mybatis MapperScannerConfigurer.java

 

最终找到 Spring的一个类  ClassPathBeanDefinitionScanner.java

参考ClassPathBeanDefinitionScanner 和它的父类 ClassPathScanningCandidateComponentProvider,将一些代码进行抽取,得到如下工具类。

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 basePackages, Class
... annotations) { return ClassScaner.scan(StringUtils.tokenizeToStringArray(basePackages, ",; \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 + org.springframework.util.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 ((includeFilters.size() == 0 && excludeFilters.size() == 0) || 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/

你可能感兴趣的文章
阿里巴巴Json工具-Fastjson教程
查看>>
Spring Cloud Gateway - 快速开始
查看>>
Java对象转JSON时如何动态的增删改查属性
查看>>
Python 面向对象进阶
查看>>
Linux常用统计命令之wc
查看>>
Git安装及使用以及连接GitHub方法详解
查看>>
docker容器与虚拟机的区别
查看>>
shell脚本里使用echo输出颜色
查看>>
Python2跟Python3的区别
查看>>
并发编程——IO模型详解
查看>>
Java之封装,继承,多态
查看>>
wait()与notify()
查看>>
使用js打印时去除页眉页脚
查看>>
Spring security OAuth2.0认证授权学习第二天(基础概念-RBAC)
查看>>
ORA-00904: "FILED_TYPE": 标识符无效
查看>>
数据仓库系列之维度建模
查看>>
Scala教程之:函数式的Scala
查看>>
java中DelayQueue的使用
查看>>
线程stop和Interrupt
查看>>
Android中定时执行任务的3种实现方法
查看>>