`
JerryWang_SAP
  • 浏览: 958067 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

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的原创文章,请关注公众号"汪子熙":

0
1
分享到:
评论

相关推荐

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下

    component-scan-spring:演示组件扫描

    component-scan-spring:演示组件扫描

    Spring 自动扫描 不支持jar包 的解决方案

    NULL 博文链接:https://xinglu.iteye.com/blog/1457029

    Spring扫描器—spring组件扫描使用详解

    NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898

    Spring注释 注入方式源码示例,Annotation

    <context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... <context:component-scan base-package="com.haijian" />

    Spring 自定义注解的解析

    在spring环境中通过component-scan配置处理自定义java注解

    Spring Context测试

    学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run project即可。 组件扫描自动进行主要在两个地方进行设置 1.applicationContext:键入 <context:annotation-config /> <context:...

    spring

    context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...

    springweb3.0MVC注解(附实例)

    <context:component-scan base-package="com.baobaotao.web"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation. ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring 3.0+Struts2+Mybatis 3 + p6spy 平台框架

    这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...

    Spring 3.0所需jar文件和对应的配置文件

    nested exception is java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are ...

    springjdbc

    <context:component-scan base-package="com.test.controller" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation....

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring AOP配置源码

    <context:component-scan base-package="com.spring.*"/> (* com.spring.service..*(..))" id="pointCut"/> <aop:before method="before" pointcut-ref="pointCut"/> <aop:after method="after" pointcut-...

    spring杂谈 作者zhang KaiTao

    1.26 context:component-scan扫描使用上的容易忽略的use-default-filters 1.27 idea内嵌jetty运行springmvc项目报ConversionFailedException 1.28 springmvc 3.2 @MatrixVariable注解 1.29 spring3.2 带matrix变量的...

    SpringMVC+Hibernate实例

    <context:component-scan base-package="com.bbs"/> <!--注解支持--> <mvc:annotation-driven/> <!--视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view....

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    <context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- <aop:config> expression="execution(* x.y.service.*...

    bundery#docs#注解1

    spring注解使用注解之前,要先在 application.xml 文件中添加<context:component-scan base-package="co

Global site tag (gtag.js) - Google Analytics