在以前文章Spring自定义标签实现中,曾说过,在sprin g 配置文件中,除了be an beans import 常用的标签意外,其他的标签都是遵循Spring 自定义标签的扩展机制进行实现功能的,

component-scan标签也不例外,关于component-scan标签的实现逻辑则是在Spring-context 包下 org.springframework.context.config.ContextNamespaceHandler 类中定义

我们看一看它的parse 解析方法:

接下来就分析它的parse 方法:主要就是三步:

  1. .找到 我们定义的 base-package 属性内容
  2. .定义扫描器
  3. .扫描包内容

第一步是拿到  base-package 属性来确定我们需要扫描的包的路径,不同的包我们可以用,;进行分割 <context:component-scan base-package="com.project" />

  1. String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute("base-package"), ",; \t\n");

第二步 确定 ClassPathBeanDefinitionScanner 扫描器,这个扫描器大有学问

  1. 我们点进去 ClassPathBeanDefinitionScanner scanner = this.configureScanner(parserContext, element);
  1. XmlReaderContext readerContext = parserContext.getReaderContext();
  2. boolean useDefaultFilters = true;
  3. if (element.hasAttribute("use-default-filters")) {
  4. useDefaultFilters = Boolean.valueOf(element.getAttribute("use-default-filters"));
  5. }
  6.  
  7. ClassPathBeanDefinitionScanner scanner = this.createScanner(readerContext, useDefaultFilters);

这里就创建了 ClassPathBeanDefinitionScanner对象,并且使用了默认的拦截器 useDefaultFilters,这个拦截器大有作用,我们往下看一些父类方法的调用

  1. return new ClassPathBeanDefinitionScanner(readerContext.getRegistry(), useDefaultFilters);
  1. this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
  1. super(useDefaultFilters, environment);

我们继续,除去一些定义:往下

  1.  
  1. if (useDefaultFilters) {
  2. registerDefaultFilters();
  3. }
  4. this.environment = environment;
  1.  

我们使用了默认的拦截器,这里的 useDefaultFilters 是true ,然后执行  registerDefaultFilters();注册默认的拦截器,我们看一看这个默认的注册方法,很关键

  1. /**
  2. * Register the default filter for {@link Component @Component}.
  3. * <p>This will implicitly register all annotations that have the
  4. * {@link Component @Component} meta-annotation including the
  5. * {@link Repository @Repository}, {@link Service @Service}, and
  6. * {@link Controller @Controller} stereotype annotations.
  7. * <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
  8. * JSR-330's {@link javax.inject.Named} annotations, if available.
  9. *
  10. */
  11. @SuppressWarnings("unchecked")
  12. protected void registerDefaultFilters() {
  13. this.includeFilters.add(new AnnotationTypeFilter(Component.class));
  14. ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
  15. try {
  16. this.includeFilters.add(new AnnotationTypeFilter(
  17. ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
  18. logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
  19. }
  20. catch (ClassNotFoundException ex) {
  21. // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
  22. }
  23. try {
  24. this.includeFilters.add(new AnnotationTypeFilter(
  25. ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
  26. logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
  27. }
  28. catch (ClassNotFoundException ex) {
  29. // JSR-330 API not available - simply skip.
  30. }
  31. }

就是上面这段代码,我们姑且先不看代码,先看一看它的注释翻译:

  1. /**
  2. * Register the default filter for {@link Component @Component}.
  3. * <p>This will implicitly register all annotations that have the
  4. * {@link Component @Component} meta-annotation including the
  5. * {@link Repository @Repository}, {@link Service @Service}, and
  6. * {@link Controller @Controller} stereotype annotations.
  7. * <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
  8. * JSR-330's {@link javax.inject.Named} annotations, if available.
  9. *
  10. */

 注册一个带有@Component 的默认拦截器,将隐士的注册所有的带有@Component 的元注解,包含@Repository @Service @Controller 注解

通过这个解释,我们可以得知,默认的拦截器会拦截带有@Component 注解的类,我们看一下@Controller @Repository @Service的定义

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Component
  5. public @interface Controller {
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Component {
  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Component
  5. public @interface Repository {

看一看出来什么呢,这三个注解都实现了@Componet 的注解,所以都会被拦截住;

其实通过它的注释我们就已经明白了,我们就看一下代码逻辑吧,这个 includeFilters 其实是一个链表

  1. private final List<TypeFilter> includeFilters = new LinkedList<TypeFilter>();

进行注册Component 拦截器:

  1. this.includeFilters.add(new AnnotationTypeFilter(Component.class));

第三步具体的逻辑大家可以往下点点;我们有了扫描器了,我们其实可以进行第三步扫描了:

  1. Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);

这个方法,扫描到匹配的class文件,装入Set<File>容器里:可以看出获取文件如果是文件目录采用了递归的方式进行下一层寻找

  1. protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException {
  2. if (logger.isDebugEnabled()) {
  3. logger.debug("Searching directory [" + dir.getAbsolutePath() +
  4. "] for files matching pattern [" + fullPattern + "]");
  5. }
  6. File[] dirContents = dir.listFiles();
  7. if (dirContents == null) {
  8. if (logger.isWarnEnabled()) {
  9. logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
  10. }
  11. return;
  12. }
  13. for (File content : dirContents) {
  14. String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
  15. if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
  16. if (!content.canRead()) {
  17. if (logger.isDebugEnabled()) {
  18. logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
  19. "] because the application is not allowed to read the directory");
  20. }
  21. }
  22. else {
  23. doRetrieveMatchingFiles(fullPattern, content, result);
  24. }
  25. }
  26. if (getPathMatcher().match(fullPattern, currPath)) {
  27. result.add(content);
  28. }
  29. }
  30. }

转换成Resource【】数组:

  1. return result.toArray(new Resource[result.size()]);

将每一个resource 封装成 MetadataReader 对象,就可以获取这个class文件的 所有信息,比如是不是接口啊,实现类等信息,这个封装过程需要好好看一下,asm的操作

  1. MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);

然后就是拦截器的判断:

  1. isCandidateComponent(metadataReader)

拦截器的过滤匹配

  1. /**
  2. * Determine whether the given class does not match any exclude filter
  3. * and does match at least one include filter.
  4. * @param metadataReader the ASM ClassReader for the class
  5. * @return whether the class qualifies as a candidate component
  6. */
  7. protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
  8. for (TypeFilter tf : this.excludeFilters) {
  9. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  10. return false;
  11. }
  12. }
  13. for (TypeFilter tf : this.includeFilters) {
  14. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  15. return isConditionMatch(metadataReader);
  16. }
  17. }
  18. return false;
  19. }
  1. if (isCandidateComponent(sbd)) {
  2. if (debugEnabled) {
  3. logger.debug("Identified candidate component class: " + resource);
  4. }
  5. candidates.add(sbd);
  6. }

通过这个的学习,大家可以去看一下Mybatis 的包扫描了,它则是继承了Spring的扫描类,但并没有使用默认的拦截器,而是配置了自己的一套拦截器及拦截接口等一些配置;

  1.  

 

Spring component-scan 标签的实现的更多相关文章

  1. [Spring Boot] Use Component Scan to scan for Bean

    Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...

  2. Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明

    Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...

  3. 基于Spring开发——自定义标签及其解析

    1. XML Schema 1.1 最简单的标签 一个最简单的标签,形式如: <bf:head-routing key="1" value="1" to= ...

  4. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  5. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  6. Spring——使用自定义标签

    文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...

  7. Spring表单标签

    虽然我们可以使用HTML原生的form表单标签来轻松的写出一个表单,其实我一直都是这样做的,但是使用Spring表单标签可以更方便我们完成例如:验证失败后表单数据的回填功能(虽然你可以使用EL+JST ...

  8. spring:使用<prop>标签为Java持久属性集注入值

    spring:使用<prop>标签为Java持久属性集注入值 使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Propertie ...

  9. not registered via @EnableConfigurationProperties or marked as Spring component

    利用@ConfigurationProperties(prefix = "")来绑定属性时报错: not registered via @EnableConfigurationPr ...

  10. Spring IoC 自定义标签解析

    前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 容 ...

随机推荐

  1. Layout-3相关代码:3列布局代码演化[二]

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Angular CLI: 全局脚本

    全局脚本 有的时候,我们需要加载全局脚本,例如 jQuery 脚本库,第三方的控件库等等.比如 jQuery 可以直接加载到 window 对象上,这就需要我们使用 Angular 中的全局脚本来处理 ...

  3. java反射应用

    package cn.sxt.TestClass; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetE ...

  4. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. zabbix3.0.4 探索主机Discovery自动发现agent主机和zabbix-agent自动注册详细图文教程

    Zabbix 自动发现(Discovery)功能使用 随着监控主机不断增多,有的时候需要添加一批机器,特别是刚用zabbix的运维人员需要将公司的所有服务器添加到zabbix,如果使用传统办法去单个添 ...

  6. 嵌入式linux——时钟(三)

    今天写第一篇,S3C2440的时钟,配置好时钟系统,各个模块才能正常有效的工作,为了了解始终系统,必须要阅读芯片手册,尽量看英文版的,这样还能捎带着增加一下阅读英语计数文档的能力. 概览 在2440数 ...

  7. mount命令和自动挂载实例

    前言 介绍mount命令和一个实例. mount命令 作用 作用:挂载linux系统外的文件 命令格式 mount [-hV] mount -a [-fFnrsvw] [-t vfstype] mou ...

  8. gitkraken clone报错 Configured SSH key is invalid

    gitkraken clone远程仓库时报错 Configured SSH key is invalid. Please confirm that is properly associated wit ...

  9. centos 7.2 同步北京时间 ,多台机器同步时间

    linux 系统没有北京时间,同步的是上海时间 linux 系统有两个时钟:一个是硬件时钟,即BIOS时间:另一个是系统时钟,是linux系统Kernel(内核)时间. 系统开启时,系统会读取硬件时间 ...

  10. php解决高并发问题

    我们通常衡量一个Web系统的吞吐率的指标是QPS(Query Per Second,每秒处理请求数),解决每秒数万次的高并发场景,这个指标非常关键.举个例子,我们假设处理一个业务请求平均响应时间为10 ...