Spring 获取一个包和子包下面所有的类

  • Post author:
  • Post category:其他



from:http://www.iteye.com/topic/203267

参考spring2.5里面的component-scan功能,提炼出这样一个工具类,此类依赖spring但不依赖spring容器,请注意此类搜索不到jdk的包如java.lang

Java代码

复制代码




  1. import



    java.io.IOException;



  2. import



    java.lang.annotation.Annotation;



  3. import



    java.util.HashSet;



  4. import



    java.util.LinkedList;



  5. import



    java.util.List;



  6. import



    java.util.Set;



  7. import



    org.springframework.beans.factory.BeanDefinitionStoreException;



  8. import



    org.springframework.context.ResourceLoaderAware;



  9. import



    org.springframework.core.io.Resource;



  10. import



    org.springframework.core.io.ResourceLoader;



  11. import



    org.springframework.core.io.support.PathMatchingResourcePatternResolver;



  12. import



    org.springframework.core.io.support.

    ResourcePatternResolver

    ;



  13. import



    org.springframework.core.io.support.ResourcePatternUtils;



  14. import



    org.springframework.core.type.classreading.CachingMetadataReaderFactory;



  15. import



    org.springframework.core.type.classreading.MetadataReader;



  16. import



    org.springframework.core.type.classreading.MetadataReaderFactory;



  17. import



    org.springframework.core.type.filter.AnnotationTypeFilter;



  18. import



    org.springframework.core.type.filter.TypeFilter;



  19. import



    org.springframework.util.SystemPropertyUtils;



  20. public






    class



    ClassScaner



    implements



    ResourceLoaderAware {



  21. private




    ResourcePatternResolver


    resourcePatternResolver

    =



    new



    PathMatchingResourcePatternResolver();



  22. private






    final



    List<TypeFilter> includeFilters =



    new



    LinkedList<TypeFilter>();



  23. private






    final



    List<TypeFilter> excludeFilters =



    new



    LinkedList<TypeFilter>();



  24. private



    MetadataReaderFactory metadataReaderFactory =



    new



    CachingMetadataReaderFactory(



  25. this



    .

    resourcePatternResolver

    );



  26. public



    ClassScaner() {
  27. }



  28. public






    void



    setResourceLoader(ResourceLoader resourceLoader) {



  29. this



    .

    resourcePatternResolver

    = ResourcePatternUtils
  30. .getResourcePatternResolver(resourceLoader);



  31. this



    .metadataReaderFactory =



    new



    CachingMetadataReaderFactory(
  32. resourceLoader);
  33. }



  34. public






    final



    ResourceLoader getResourceLoader() {



  35. return






    this



    .

    resourcePatternResolver

    ;
  36. }



  37. public






    void



    addIncludeFilter(TypeFilter includeFilter) {



  38. this



    .includeFilters.add(includeFilter);
  39. }



  40. public






    void



    addExcludeFilter(TypeFilter excludeFilter) {



  41. this



    .excludeFilters.add(


    0


    , excludeFilter);
  42. }



  43. public






    void



    resetFilters(



    boolean



    useDefaultFilters) {



  44. this



    .includeFilters.clear();



  45. this



    .excludeFilters.clear();
  46. }



  47. public






    static



    Set<Class> scan(String basePackage,
  48. Class<?



    extends



    Annotation>… annotations) {
  49. ClassScaner cs =



    new



    ClassScaner();



  50. for



    (Class anno : annotations)
  51. cs.addIncludeFilter(



    new



    AnnotationTypeFilter(anno));



  52. return



    cs.doScan(basePackage);
  53. }



  54. public






    static



    Set<Class> scan(String[] basePackages,
  55. Class<?



    extends



    Annotation>… annotations) {
  56. ClassScaner cs =



    new



    ClassScaner();



  57. for



    (Class anno : annotations)
  58. cs.addIncludeFilter(



    new



    AnnotationTypeFilter(anno));
  59. Set<Class> classes =



    new



    HashSet<Class>();



  60. for



    (String s : basePackages)
  61. classes.addAll(cs.doScan(s));



  62. return



    classes;
  63. }



  64. public



    Set<Class> doScan(String basePackage) {
  65. Set<Class> classes =



    new



    HashSet<Class>();



  66. try



    {
  67. String packageSearchPath =

    ResourcePatternResolver

    .CLASSPATH_ALL_URL_PREFIX
  68. + org.springframework.util.ClassUtils
  69. .convertClassNameToResourcePath(SystemPropertyUtils
  70. .resolvePlaceholders(basePackage))
  71. +


    “/**/*.class”


    ;
  72. Resource[] resources =



    this



    .

    resourcePatternResolver
  73. .getResources(packageSearchPath);



  74. for



    (



    int



    i =


    0


    ; i < resources.length; i++) {
  75. Resource resource = resources[i];



  76. if



    (resource.isReadable()) {
  77. MetadataReader metadataReader =



    this



    .metadataReaderFactory
  78. .getMetadataReader(resource);



  79. if



    ((includeFilters.size() ==


    0


    && excludeFilters.size() ==


    0


    )
  80. || matches(metadataReader)) {



  81. try



    {
  82. classes.add(Class.forName(metadataReader
  83. .getClassMetadata().getClassName()));
  84. }



    catch



    (ClassNotFoundException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }
  90. }



    catch



    (IOException ex) {



  91. throw






    new



    BeanDefinitionStoreException(


  92. “I/O failure during classpath scanning”


    , ex);
  93. }



  94. return



    classes;
  95. }



  96. protected






    boolean



    matches(MetadataReader metadataReader)



    throws



    IOException {



  97. for



    (TypeFilter tf :



    this



    .excludeFilters) {



  98. if



    (tf.match(metadataReader,



    this



    .metadataReaderFactory)) {



  99. return






    false



    ;
  100. }
  101. }



  102. for



    (TypeFilter tf :



    this



    .includeFilters) {



  103. if



    (tf.match(metadataReader,



    this



    .metadataReaderFactory)) {



  104. return






    true



    ;
  105. }
  106. }



  107. return






    false



    ;
  108. }
  109. }