欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring里component-scan的工作原理 JAVAJ2EESpringannotation注解 

程序员文章站 2022-03-04 16:18:45
...

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.

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

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:

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

And the actual scan is performed in line 87:

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

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:

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

The evaluation to check whether the class has qualified annotation is done in this method:

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

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.

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

By inspecting content of this.includeFilters, we can know that Spring framework considers @Component and @Named as qualified annotation for automatic component scan logic.

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

Back to my example, since my bean class has @named annotated,

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

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.

 

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解 

 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Spring里component-scan的工作原理
            
    
    
        JAVAJ2EESpringannotation注解