packagecn.com.hithere.web.listener;importjava.io.IOException;importjava.lang.annotation.Annotation;importjava.util.HashSet;importjava.util.LinkedList;importjava.util.List;importjava.util.Set;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframework.core.io.Resource;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternResolver;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.ClassUtils;public classLoadPackageClasses {protected final Log logger =LogFactory.getLog(getClass());private static final String RESOURCE_PATTERN = "/**/*.class";private ResourcePatternResolver resourcePatternResolver = newPathMatchingResourcePatternResolver();private List packagesList= new LinkedList();private List typeFilters = new LinkedList();private Set> classSet= new HashSet>();/*** 构造函数
*@parampackagesToScan 指定哪些包需要被扫描,支持多个包"package.a,package.b"并对每个包都会递归搜索
*@paramannotationFilter 指定扫描包中含有特定注解标记的bean,支持多个注解*/
public LoadPackageClasses(String[] packagesToScan, Class extends Annotation>... annotationFilter){if (packagesToScan != null) {for(String packagePath : packagesToScan) {this.packagesList.add(packagePath);
}
}if (annotationFilter != null){for (Class extends Annotation>annotation : annotationFilter) {
typeFilters.add(new AnnotationTypeFilter(annotation, false));
}
}
}/*** 将符合条件的Bean以Class集合的形式返回
*@return*@throwsIOException
*@throwsClassNotFoundException*/
public Set> getClassSet() throwsIOException, ClassNotFoundException {this.classSet.clear();if (!this.packagesList.isEmpty()) {for (String pkg : this.packagesList) {
String pattern= ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +ClassUtils.convertClassNameToResourcePath(pkg)+RESOURCE_PATTERN;
Resource[] resources= this.resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory= new CachingMetadataReaderFactory(this.resourcePatternResolver);for(Resource resource : resources) {if(resource.isReadable()) {
MetadataReader reader=readerFactory.getMetadataReader(resource);
String className=reader.getClassMetadata().getClassName();if(matchesEntityTypeFilter(reader, readerFactory)) {this.classSet.add(Class.forName(className));
}
}
}
}
}//输出日志
if(logger.isInfoEnabled()){for (Class> clazz : this.classSet) {
logger.info(String.format("Found class:%s", clazz.getName()));
}
}return this.classSet;
}/*** 检查当前扫描到的Bean含有任何一个指定的注解标记
*@paramreader
*@paramreaderFactory
*@return*@throwsIOException*/
private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throwsIOException {if (!this.typeFilters.isEmpty()) {for (TypeFilter filter : this.typeFilters) {if(filter.match(reader, readerFactory)) {return true;
}
}
}return false;
}
}