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的原创文章,请关注公众号"汪子熙":
相关推荐
主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下
component-scan-spring:演示组件扫描
NULL 博文链接:https://xinglu.iteye.com/blog/1457029
NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
//applicationContext.xml文件中添加 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... <context:component-scan base-package="com.haijian" />
在spring环境中通过component-scan配置处理自定义java注解
学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run project即可。 组件扫描自动进行主要在两个地方进行设置 1.applicationContext:键入 <context:annotation-config /> <context:...
context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...
<context:component-scan base-package="com.baobaotao.web"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation. ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...
nested exception is java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are ...
<context:component-scan base-package="com.test.controller" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation....
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
<context:component-scan base-package="com.spring.*"/> (* com.spring.service..*(..))" id="pointCut"/> <aop:before method="before" pointcut-ref="pointCut"/> <aop:after method="after" pointcut-...
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变量的...
<context:component-scan base-package="com.bbs"/> <!--注解支持--> <mvc:annotation-driven/> <!--视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view....
<context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- <aop:config> expression="execution(* x.y.service.*...
spring注解使用注解之前,要先在 application.xml 文件中添加<context:component-scan base-package="co