Spring裡component-scan的工作原理
In Spring configuration xml file, we can define a package for tag component-scan, which tells Spring framework to search all classes within this specified package, to look for those classes which are annotated with @Named or @Component.
I am very curious about how Spring framework achieves this scan, so I have made some debugging to figure it out.
In this blog How to find the exact location where bean configuration file is parsed in Spring framework I have already found the location where the xml configuration file is parsed by Spring framework, so I can directly set breakpoint in found source code. Here the package to be scanned is parsed from xml file:
And the actual scan is performed in line 87:
Here all classes within the specified package and its children packages are extracted as resource, now I have 7 resources as candidates for scan which makes sense since I have totally 7 classes in the package:
The evaluation to check whether the class has qualified annotation is done in this method:
If the scanned class has at least one annotation ( the annotation written on class is stored in metsadataReader ) which resides in this.includeFilters, then it is considered as a candidate.
By inspecting content of this.includeFilters, we can know that Spring framework considers @Component and @Named as qualified annotation for automatic component scan logic.
Back to my example, since my bean class has @named annotated,
In the runtime, this annotation written in class source code is extracted via reflection, and checked against Spring framework pre-defined annotation set. Here below is how my bean class evaluated as candidate, since it has @Named annotation.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2724093/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring框架裡註解@Autowired的工作原理Spring框架
- spring-mvc的工作原理SpringMVC
- Spring Session工作原理SpringSession
- SAP GUI裡Screen Painter的工作原理GUIAI
- Spring WebFlux 的設計及工作原理剖析SpringWebUX
- 深入理解:Spring MVC工作原理SpringMVC
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- Spring 系列(四):我們來聊聊<context:component-scan/>SpringContext
- ?【Spring專題】「原理系列」SpringMVC的執行工作原理(補充修訂)SpringMVC
- Spring Cloud教程 第三彈 Ribbon工作原理SpringCloud
- Angular單元測試框架裡API toHaveBeenCalledTimes的工作原理Angular框架API
- Mirror 的工作原理
- LiveData的工作原理LiveData
- OAuth的工作原理OAuth
- Feign的工作原理
- Spark的工作原理Spark
- Spring MVC的工作原理,我們來看看其原始碼實現SpringMVC原始碼
- 就想搞明白,component-scan 是怎麼把Bean都註冊到Spring容器的!BeanSpring
- SAP Fiori Elements 框架裡 Smart Table 控制元件的工作原理介紹框架控制元件
- Android View 的工作原理AndroidView
- HTTPS代理的工作原理HTTP
- SOCKS代理的工作原理
- OSPF的基本工作原理
- <context:component-scan> 標籤Context
- spring應用手冊-IOC(XML配置實現)-(36)-context:component-scan的scope-resolver屬性SpringXMLContext
- spring應用手冊-IOC(XML配置實現)-(35)-context:component-scan的scoped-proxy屬性SpringXMLContext
- 工作流引擎的工作原理與功能
- 深入介紹 UI5 框架裡 Smart Field 控制元件的工作原理UI框架控制元件
- 深度學習的工作原理深度學習
- PHP 中的 foreach 工作原理PHP
- 基本的爬蟲工作原理爬蟲
- 介紹GitOps的工作原理Git
- WireGuard 教程:WireGuard 的工作原理
- Android View的工作原理(上)AndroidView
- SSH反向隧道的工作原理
- SAP Spartacus NgExpressEngineDecorator 的工作原理Express
- KubernetesAPIserver工作原理APIServer
- Mybatis工作原理MyBatis