java 获取包名类名_Spring根据包名获取包路径下的所有类

importorg.apache.commons.lang3.ArrayUtils;importorg.springframework.beans.factory.BeanDefinitionStoreException;importorg.springframework.context.ResourceLoaderAware;importorg.springframework.core.io.Resource;importorg.springframework.core.io.ResourceLoader;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternUtils;importorg.springframework.core.type.classreading.CachingMetadataReaderFactory;importorg.springframework.core.type.classreading.MetadataReader;importorg.springframework.core.type.classreading.MetadataReaderFactory;importorg.springframework.core.type.filter.AnnotationTypeFilter;importorg.springframework.core.type.filter.TypeFilter;importorg.springframework.util.StringUtils;importorg.springframework.util.SystemPropertyUtils;importjava.io.IOException;importjava.lang.annotation.Annotation;importjava.util.HashSet;importjava.util.LinkedList;importjava.util.List;importjava.util.Set;public class ClassScaner implementsResourceLoaderAware {//保存过滤规则要排除的注解

private final List includeFilters = new LinkedList();private final List excludeFilters = new LinkedList();private ResourcePatternResolver resourcePatternResolver = newPathMatchingResourcePatternResolver();private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);public static Setscan(String[] basePackages,

Class extends Annotation>... annotations) {

ClassScaner cs= newClassScaner();if(ArrayUtils.isNotEmpty(annotations)) {for(Class anno : annotations) {

cs.addIncludeFilter(newAnnotationTypeFilter(anno));

}

}

Set classes = new HashSet();for(String s : basePackages)

classes.addAll(cs.doScan(s));returnclasses;

}public static Set scan(String basePackages, Class extends Annotation>... annotations) {return ClassScaner.scan(StringUtils.tokenizeToStringArray(basePackages, ",; \t\n"), annotations);

}public finalResourceLoader getResourceLoader() {return this.resourcePatternResolver;

}public voidsetResourceLoader(ResourceLoader resourceLoader) {this.resourcePatternResolver =ResourcePatternUtils

.getResourcePatternResolver(resourceLoader);this.metadataReaderFactory = newCachingMetadataReaderFactory(

resourceLoader);

}public voidaddIncludeFilter(TypeFilter includeFilter) {this.includeFilters.add(includeFilter);

}public voidaddExcludeFilter(TypeFilter excludeFilter) {this.excludeFilters.add(0, excludeFilter);

}public void resetFilters(booleanuseDefaultFilters) {this.includeFilters.clear();this.excludeFilters.clear();

}public SetdoScan(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 newBeanDefinitionStoreException("I/O failure during classpath scanning", ex);

}returnclasses;

}protected boolean matches(MetadataReader metadataReader) throwsIOException {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 voidmain(String[] args) {

ClassScaner.scan("com.hjzgg.test.thirdjar.config,com.hjzgg.test.thirdjar.web", null)

.forEach(clazz->System.out.println(clazz));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值