当前位置:   article > 正文

复用代码系列:类扫描(基于Spring)_spring classscane

spring classscane

复用代码系列目录

  1. package com.spring.custom_scanner;
  2. import java.io.IOException;
  3. import java.lang.annotation.Annotation;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Set;
  8. import org.springframework.beans.factory.BeanDefinitionStoreException;
  9. import org.springframework.context.ResourceLoaderAware;
  10. import org.springframework.core.io.Resource;
  11. import org.springframework.core.io.ResourceLoader;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import org.springframework.core.io.support.ResourcePatternResolver;
  14. import org.springframework.core.io.support.ResourcePatternUtils;
  15. import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
  16. import org.springframework.core.type.classreading.MetadataReader;
  17. import org.springframework.core.type.classreading.MetadataReaderFactory;
  18. import org.springframework.core.type.filter.AnnotationTypeFilter;
  19. import org.springframework.core.type.filter.TypeFilter;
  20. import org.springframework.util.SystemPropertyUtils;
  21. public class ClassScaner implements ResourceLoaderAware {
  22. private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
  23. private final List<TypeFilter> includeFilters = new LinkedList<TypeFilter>();
  24. private final List<TypeFilter> excludeFilters = new LinkedList<TypeFilter>();
  25. private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
  26. public ClassScaner() {
  27. }
  28. public void setResourceLoader(ResourceLoader resourceLoader) {
  29. this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
  30. this.metadataReaderFactory = new CachingMetadataReaderFactory( resourceLoader);
  31. }
  32. public final ResourceLoader getResourceLoader() {
  33. return this.resourcePatternResolver;
  34. }
  35. public void addIncludeFilter(TypeFilter includeFilter) {
  36. this.includeFilters.add(includeFilter);
  37. }
  38. public void addExcludeFilter(TypeFilter excludeFilter) {
  39. this.excludeFilters.add(0, excludeFilter);
  40. }
  41. public void resetFilters(boolean useDefaultFilters) {
  42. this.includeFilters.clear();
  43. this.excludeFilters.clear();
  44. }
  45. public static Set<Class> scan(String basePackage, Class<? extends Annotation>... annotations) {
  46. ClassScaner cs = new ClassScaner();
  47. for (Class anno : annotations)
  48. cs.addIncludeFilter(new AnnotationTypeFilter(anno));
  49. return cs.doScan(basePackage);
  50. }
  51. public static Set<Class> scan(String[] basePackages, Class<? extends Annotation>... annotations) {
  52. ClassScaner cs = new ClassScaner();
  53. for (Class anno : annotations)
  54. cs.addIncludeFilter(new AnnotationTypeFilter(anno));
  55. Set<Class> classes = new HashSet<Class>();
  56. for (String s : basePackages)
  57. classes.addAll(cs.doScan(s));
  58. return classes;
  59. }
  60. public Set<Class> doScan(String basePackage) {
  61. Set<Class> classes = new HashSet<Class>();
  62. try {
  63. String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
  64. + org.springframework.util.ClassUtils
  65. .convertClassNameToResourcePath(SystemPropertyUtils
  66. .resolvePlaceholders(basePackage))
  67. + "/**/*.class";
  68. Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
  69. for (int i = 0; i < resources.length; i++) {
  70. Resource resource = resources[i];
  71. if (resource.isReadable()) {
  72. MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
  73. if ((includeFilters.size() == 0 && excludeFilters.size() == 0)
  74. || matches(metadataReader)) {
  75. try {
  76. classes.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
  77. } catch (ClassNotFoundException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. }
  83. } catch (IOException ex) {
  84. throw new BeanDefinitionStoreException(
  85. "I/O failure during classpath scanning", ex);
  86. }
  87. return classes;
  88. }
  89. protected boolean matches(MetadataReader metadataReader) throws IOException {
  90. for (TypeFilter tf : this.excludeFilters) {
  91. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  92. return false;
  93. }
  94. }
  95. for (TypeFilter tf : this.includeFilters) {
  96. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/240321
推荐阅读
相关标签
  

闽ICP备14008679号