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));
}
}