Insight context:component-scan 实现class scan

来源:互联网 发布:c语言二分法求方程 编辑:程序博客网 时间:2024/05/04 08:07

疑问:spring 配置component-scan 是怎样实现类的扫描的?

...

需要准备的知识:Insight spring 常识

...

按照以往的分析,context:xxx配置的解析由ContextNamespaceHandler 完成。然后找到component-scan的解析器:ComponentScanBeanDefinitionParser。

对于context:component-scan 的配置,本文只做base-package 以及exclude-filter、include-filter的分析。

...

1、解析配置scanner 的过程如下:

...

2、scan 的步骤:

...

总结:

scan 的过程就是指定"路径" 下class文件的遍历过程,然后对asm解析Class-meta 进行过滤得到符合的bean-definition。

filter体现@Component 注解的作用。

...

设计借鉴:

1、黑白名单的业务过滤可以参考spring scan-filter 的设计,不同的策略创建不同的TypeFilter,过滤的处理过程直接委托给各自的TypeFilter逻辑去实现。

2、优先级处理可以参考isCandidateComponent 进行。

3、根据运行的环境(classpath)进行功能的自适应,参考registerDefaultFilters。

0 0
原创粉丝点击