博客
关于我
Spring根据包名获取包路径下的所有类
阅读量:428 次
发布时间: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/

你可能感兴趣的文章
mysql的sql语句基本练习
查看>>
mysql自增id超大问题查询
查看>>
mysql进阶 with-as 性能调优
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL集群解决方案(4):负载均衡
查看>>
mysql面试题学校三表查询_mysql三表查询分组后取每组最大值,mysql面试题。
查看>>
mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
查看>>
MYSQL高可用集群MHA架构
查看>>
MySQL高级-MySQL并发参数调整
查看>>
MySQL高级-SQL优化步骤
查看>>
MySQL高级-视图
查看>>
mysql高级查询~分页查询
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
nacos config
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置自动刷新源码解析
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>