Spring中用烂了的@Component注解是怎么实现的呢?

开发 前端
经过这篇文章文章,是不是对@Component​的使用和实现原理一清二楚了呢,其实Spring​中还有@Service、@Controller和@Repository​等注解,他们和@Component有什么区别呢,你知道吗?

概述

想必@Component注解大家一直在使用,只要类上加上它,就可以被Spring容器管理,那大家有想过它是怎么实现的吗?本篇文章就带领到家揭秘。

注解介绍

用来标记的类是一个“组件”或者说是一个Bean,Spring会自动扫描标记@Component注解的类作为一个Spring Bean对象。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

 /**
  * The value may indicate a suggestion for a logical component name,
  * to be turned into a Spring bean in case of an autodetected component.
  * @return the suggested component name, if any (or empty String otherwise)
  */
 String value() default "";

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

属性说明:

  • value: 自定义当前组件或者说bean的名称,可以不配置, 不配置的话默认为组件的首字母小写的类名。

元注解说明:

  • 该注解只能使用在类,接口、枚举、其他注解上
  • 该注解的生命周期是运行时JVM
  • @Indexed​元注解在spring 5.0引入,用于项目编译打包时,会在自动生成META-INF/spring.components文件,简历索引,从而提高组件扫描效率,减少应用启动时间。

注解使用

  1. 定义Person类,被@Component注解修饰

图片

  1. 检查Person类是否在扫描路径下

图片

  1. 获取bean验证

图片

图片

小结: 通过添加@Component能够将类转为Spring中的Bean对象,前提是能过够被扫描到。

原理解析

阅读源码,我们查看@Component​注解的源码,从中可以看到一个关键的类ClassPathBeanDefinitionScanner,我们可以从这个类下手,找到切入点。

图片

分析ClassPathBeanDefinitionScanner​类,找到核心方法doscan, 打个断点,了解整个调用链路。

图片

具体分析结果如下:

  1. SpringBoot​应用启动会注册ConfigurationClassPostProcessor这个Bean,它实现了BeanDefinitionRegistryPostProcessor接口,而这个接口是Spring提供的一个扩展点,可以往BeanDefinition Registry中添加BeanDefintion。所以,只要能够扫描到@Component注解的类,并且把它注册到BeanDefinition Registry中即可。

图片

  1. 关键方法ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry,查找@Component的类,并进行注册。

图片

  1. 我们直接跳到是如何查找@Component的类的,核心方法就是ClassPathBeanDefinitionScanner#doScan。
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
  Assert.notEmpty(basePackages, "At least one base package must be specified");
  Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
        // 遍历多个扫描目录,如本例中的com.alvinlkk
  for (String basePackage : basePackages) {
            // 核心方法查找所有符合条件的BeanDefinition, 该方法后面重点关注
   Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
   // 遍历找到的BeanDefinition
            for (BeanDefinition candidate : candidates) {
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
    candidate.setScope(scopeMetadata.getScopeName());
    String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
    if (candidate instanceof AbstractBeanDefinition) {
     postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
    }
    if (candidate instanceof AnnotatedBeanDefinition) {
     AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
    }
                // 验证BeanDefinition
    if (checkCandidate(beanName, candidate)) {
     BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
     definitionHolder =
       AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
     beanDefinitions.add(definitionHolder);
                    // 注册BeanDefinition到registry中
     registerBeanDefinition(definitionHolder, this.registry);
    }
   }
  }
  return beanDefinitions;
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  1. 重点关注ClassPathBeanDefinitionScanner#findCandidateComponents方法,找出候选的Bean Component。
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
  // 判断组件是否加了索引,打包后默认会有索引,用于加快扫描
        if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
   return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
  }
       // 重点查看else逻辑
  else {
   return scanCandidateComponents(basePackage);
  }
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
  Set<BeanDefinition> candidates = new LinkedHashSet<>();
  try {
            // 解析出需要扫描的路径,本例是classpath*:com/alvinlkk/**/*.class
   String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
     resolveBasePackage(basePackage) + '/' + this.resourcePattern;
   // 根据扫描路径找到所有的Resource
            Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
   boolean traceEnabled = logger.isTraceEnabled();
   boolean debugEnabled = logger.isDebugEnabled();
            // 遍历扫描路径
   for (Resource resource : resources) {
    if (traceEnabled) {
     logger.trace("Scanning " + resource);
    }
    try {
                    // 解析出扫描到类的元数据信息,里面包含了注解信息
     MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
     // 关键方法,判断是否候选组件
                    if (isCandidateComponent(metadataReader)) {
      ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
      sbd.setSource(resource);
      if (isCandidateComponent(sbd)) {
       if (debugEnabled) {
        logger.debug("Identified candidate component class: " + resource);
       }
       candidates.add(sbd);
      }
      else {
       if (debugEnabled) {
        logger.debug("Ignored because not a concrete top-level class: " + resource);
       }
      }
     }
     else {
      if (traceEnabled) {
       logger.trace("Ignored because not matching any filter: " + resource);
      }
     }
    }
    catch (FileNotFoundException ex) {
     if (traceEnabled) {
      logger.trace("Ignored non-readable " + resource + ": " + ex.getMessage());
     }
    }
    catch (Throwable ex) {
     throw new BeanDefinitionStoreException(
       "Failed to read candidate component class: " + resource, ex);
    }
   }
  }
  catch (IOException ex) {
   throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
  }
  return candidates;
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
// 判断是否候选的Bean Component
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
  // exclude过滤器,在exclude过滤其中的,会直接排除掉,返回false
        for (TypeFilter tf : this.excludeFilters) {
   if (tf.match(metadataReader, getMetadataReaderFactory())) {
    return false;
   }
  }
       // include过滤器, 这里会看到有AnnotationTypeFilter,注解类型过滤器
  for (TypeFilter tf : this.includeFilters) {
            // 调用AnnotationTypeFilter的match方法,来判断是否满足条件
   if (tf.match(metadataReader, getMetadataReaderFactory())) {
                // 下面在进行Condition的判断,就是类上的@Conditional,这里不是重点
    return isConditionMatch(metadataReader);
   }
  }
  return false;
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

图片

而这个AnnotationTypeFilter默认是在构造函数中注册进去的。

图片

小结:

@Component到Spring bean容器管理过程如下:

  1. 初始化时设置了Component类型过滤器;
  2. 根据指定扫描包扫描.class文件,生成Resource对象;
  3. 解析.class文件并注解归类,生成MetadataReader对象;
  4. 使用第一步的注解过滤器过滤出有@Component类;
  5. 生成BeanDefinition对象;
  6. 把BeanDefinition注册到Spring容器。

总结

经过这篇文章文章,是不是对@Component​的使用和实现原理一清二楚了呢,其实Spring​中还有@Service、@Controller和@Repository​等注解,他们和@Component有什么区别呢,你知道吗?

责任编辑:武晓燕 来源: JAVA旭阳
相关推荐

2022-12-21 07:56:30

@Component注解派生性

2025-02-28 08:16:14

Spring框架注解

2017-10-13 14:35:09

Java注解上位

2022-06-28 14:57:09

FormatterSpring

2021-06-26 14:59:13

SpringTransaction执行

2023-05-08 08:11:49

@Component使用场景时序图

2024-08-13 09:26:07

2024-05-31 09:31:00

2022-07-27 13:06:50

MySQL数据库命令

2019-04-15 08:32:25

Spring Boot日志门面模式

2021-08-30 22:38:47

VscodeMarkdown预览

2020-09-03 11:04:20

Spring 循环依赖

2021-03-08 00:11:02

Spring注解开发

2021-06-01 05:50:03

Spring@PostConstrLifecycle

2009-06-24 14:47:23

Spring MVC

2009-02-17 18:52:06

网络虚拟化路由系统数据中心

2024-09-18 13:49:42

2021-04-19 11:23:29

操作系统计算机DOS

2023-11-23 19:30:35

Python编程语言

2017-11-17 09:13:31

Java注解
点赞
收藏

51CTO技术栈公众号