SpringBoot 下使用 @Import、ImportSelector 实现自定义规则 Bean 注册

1 Spring 内置的扫描器

使用XML 配置 Spring 组件扫描使用的是 component-scan 标签,其底层使用 ClassPathBeanDefinitionScanner 这个类完成扫描工作的。
到了 SpringBoot 这里,可以使用注解 @ComponentScan 注解配合 @Configuration@Service@Repository@Controller 等注解使用,底层则使用了 ComponentScanAnnotationParser 解析器完成解析工作。

1.1 实现原理

ComponentScanAnnotationParser 解析器内部使用了 ClassPathBeanDefinitionScanner 扫描器.
ClassPathBeanDefinitionScanner扫描器内部的处理过程整理如下:

遍历 basePackages,找出这个包下的所有的 class。找出之后封装成 Resource 接口集合,这个 Resource 接口是 Spring 对资源的封装,有 FileSystemResource、ClassPathResource、UrlResource 等实现。遍历找到的 Resource 集合,通过 includeFiltersexcludeFilters 判断是否解析。这里的 includeFiltersexcludeFiltersTypeFilter 接口类型的集合,是 ClassPathBeanDefinitionScanner 内部的属性。
TypeFilter 接口是一个用于判断类型是否满足要求的类型过滤器。excludeFilters 中只要有一个TypeFilter满足条件,这个Resource就会被过滤。includeFilters 中只要有一个TypeFilter满足条件,这个Resource就会被保留,最后把保留的 Resource 封装成 ScannedGenericBeanDefinition 添加到 BeanDefinition 结果集中。

TypeFilter接口的定义:

public interface TypeFilter {
   
	boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException;
}

TypeFilter 常用实现:

  • AnnotationTypeFilter:类是否有注解修饰
  • RegexPatternTypeFilter:类名是否满足正则表达式。

ClassPathBeanDefinitionScanner 继承 ClassPathScanningCandidateComponentProvider

ClassPathScanningCandidateComponentProvider 内部的构造函数提供了一个 useDefaultFilters 参数,这个参数表示是否使用默认的TypeFilter,如果设置为true,会添加默认的TypeFilter:

public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters) {
   
  this(useDefaultFilters, new StandardEnvironment());
}

// 默认 TypeFilter
protected void registerDefaultFilters() {
   
	  this.includeFilters.add(new AnnotationTypeFilter(Component.class));
	  ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
	  try {
   
	    this.includeFilters.add(new AnnotationTypeFilter(
	        ((
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值