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
- SAP GUI裡Screen Painter的工作原理GUIAI
- Spring Session工作原理SpringSession
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- Spring WebFlux 的設計及工作原理剖析SpringWebUX
- 深入理解:Spring MVC工作原理SpringMVC
- Angular單元測試框架裡API toHaveBeenCalledTimes的工作原理Angular框架API
- ?【Spring專題】「原理系列」SpringMVC的執行工作原理(補充修訂)SpringMVC
- Spring 系列(四):我們來聊聊<context:component-scan/>SpringContext
- Spring Cloud教程 第三彈 Ribbon工作原理SpringCloud
- SAP Fiori Elements 框架裡 Smart Table 控制元件的工作原理介紹框架控制元件
- Mirror 的工作原理
- Spark的工作原理Spark
- View的工作原理View
- HashMap的工作原理HashMap
- DHCP的工作原理
- tcmalloc的工作原理
- undo的工作原理
- OAuth的工作原理OAuth
- 深入介紹 UI5 框架裡 Smart Field 控制元件的工作原理UI框架控制元件
- Spring MVC的工作原理,我們來看看其原始碼實現SpringMVC原始碼
- Feign的工作原理
- OSPF的基本工作原理
- LiveData的工作原理LiveData
- SOCKS代理的工作原理
- Go的web工作原理GoWeb
- Web Service 的工作原理Web
- ThreadLocal的工作原理thread
- tcmalloc的工作原理(一)
- Java HashMap的工作原理JavaHashMap
- Standby Database的工作原理Database
- 堆疊的工作原理
- 天線的工作原理
- context:component-scan標籤的use-default-filters屬性的作用以及原理分析ContextFilter
- 就想搞明白,component-scan 是怎麼把Bean都註冊到Spring容器的!BeanSpring
- 工作流引擎的工作原理與功能
- require工作原理UI