Loading

深入理解ClassPathBeanDefinitionScanner

1 介绍

ClassPathBeanDefinitionScanner可以扫描指定路径下的@Component类,将这些类解析成BeanDefinition,注册到Spring容器中。

此外,ClassPathBeanDefinitionScanner通过注册相关注解后处理器,提供了对@Order@Priority@Autowired@Configuration@EventListener等注解的支持。

ClassPathScanningCandidateComponentProvider成员变量:

  • resourcePattern:资源文件的路径匹配模式,默认是**/*.class,表示会扫描所有类文件。
  • includeFilters:过滤器,满足条件的类会被注册成bean。默认条件为标注@Component@ManagedBean@Named注解,后两个条件成立需要引入相关依赖。
  • excludeFilters:过滤器,满足条件的类会被跳过,不会被注册成bean。默认为空。
  • environment:运行时环境,可获取系统变量和配置文件信息。
  • conditionEvaluator:会根据@Conditional注解判断是否需要注册成bean
  • resourcePatternResolver:根据路径匹配模式获取类文件的Resource对象数组。
  • metadataReaderFactoryMetadataReader工厂,用来读取类文件的元数据。
  • componentsIndex

ClassPathBeanDefinitionScanner成员变量:

  • registryBeanDefinition注册器,实际上就是Spring容器。
  • beanDefinitionDefaultsBeanDefinition默认属性的封装工具。
  • autowireCandidatePatterns
  • beanNameGeneratorbeanName生成器,会先获取@Component@ManagedBean@Named@Component子注解的value属性,没有再按类名生成。
  • scopeMetadataResolver:作用域解析器,会先获取@Scope注解的valueproxyMode属性,没有则使用默认值(singletonScopedProxyMode.NO)。
  • includeAnnotationConfig:是否往Spring容器注册默认的XxxProcessor,默认为true

2 基本使用

在使用ClassPathBeanDefinitionScanner时,首先需要为其设置registry属性,通常通过构造函数进行设置。

DefaultListableBeanFactoryGenericApplicationContext都实现了BeandefinitionRegistry接口,Spring容器实现类基本上都可以作为registry设置到ClassPathBeanDefinitionScanner中。

然后,通过ClassPathBeanDefinitionScanner#scan()就可以扫描指定包下的所有类文件,将符合条件的类作为BeanDefinition,注册到registry中。

以下是使用ClassPathBeanDefinitionScanner扫描指定包下所有bean的最基本使用:

// 创建registry  
BeanDefinitionRegistry registry = new DefaultListableBeanFactory();  
// 创建scanner,设置registry  
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);  
// 扫描指定包下的bean  
scanner.scan("com.example.component");  
// 获取bean  
BeanFactory beanFactory = (BeanFactory) registry;  
Object bean = beanFactory.getBean("xxx");

3 源码解读

3.1 构造函数初始化

我们通常会使用ClassPathBeanDefinitionScanner(BeanDefinitionRegistry)构造函数进行初始化,为其指定registry属性:

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {  
   this(registry, true);  
}

在这个过程中,除了registry,还会初始化includeFiltersenvironmentresourcePatternResolvermetadataReaderFactorycomponentsIndex等属性:

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,  
      Environment environment, @Nullable ResourceLoader resourceLoader) {  
  
   Assert.notNull(registry, "BeanDefinitionRegistry must not be null");  
   // 初始化registry
   this.registry = registry;  
   // 初始化includeFilters:添加@Component、@ManagedBean和@Named条件
   if (useDefaultFilters) {  
      registerDefaultFilters();  
   }  
   // 初始化environment
   setEnvironment(environment);  
   // 初始化resourcePatternResolver、metadataReaderFactory和componentsIndex
   setResourceLoader(resourceLoader);  
}

ClassPathScanningCandidateComponentProvider#registerDefaultFilters()方法中,会添加校验@Component@ManagedBean@Named三个条件的注解过滤器。需要注意的是,后两个过滤器只有在引入相关依赖的时候才会生效:

protected void registerDefaultFilters() {  
   this.includeFilters.add(new AnnotationTypeFilter(Component.class));  
   ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();  
   try {  
      this.includeFilters.add(new AnnotationTypeFilter(  
            ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));  
      logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");  
   }  
   catch (ClassNotFoundException ex) {  
      // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.  
   }  
   try {  
      this.includeFilters.add(new AnnotationTypeFilter(  
            ((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));  
      logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");  
   }  
   catch (ClassNotFoundException ex) {  
      // JSR-330 API not available - simply skip.  
   }  
}

ClassPathScanningCandidateComponentProvider#setResourceLoader()方法中,会初始化resourcePatternResolvermetadataReaderFactorycomponentsIndex

public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {  
   this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);  
   this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);  
   this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(this.resourcePatternResolver.getClassLoader());  
}

在初始化resourcePatternResolver时,会判断registry是否是ResourcePatternResolver实现类(因为ApplicationContext继承了ResourcePatternResolver接口,而DefaultListableBeanFactory则没有)。如果是,直接将registry赋值给resourcePatternResolver;如果不是,则会新建一个PathMatchingResourcePatternResolver对象:

public static ResourcePatternResolver getResourcePatternResolver(@Nullable ResourceLoader resourceLoader) {  
   if (resourceLoader instanceof ResourcePatternResolver) {  
      return (ResourcePatternResolver) resourceLoader;  
   }  
   else if (resourceLoader != null) {  
      return new PathMatchingResourcePatternResolver(resourceLoader);  
   }  
   else {  
      return new PathMatchingResourcePatternResolver();  
   }  
}

在初始化componentsIndex时,会尝试读取META-INF/spring.components文件中定义的配置信息,后续扫描时会从配置信息里获取符合条件的bean进行加载:

private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {  
   // 读取spring.index.ignore配置,需要关闭spring.components功能时可以将设为false
   if (shouldIgnoreIndex) {  
      return null;  
   }  
  
   try {  
      // 读取META-INF/spring.components
      Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);  
      if (!urls.hasMoreElements()) {  
         return null;  
      }  
      // 获取配置
      List<Properties> result = new ArrayList<>();  
      while (urls.hasMoreElements()) {  
         URL url = urls.nextElement();  
         Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));  
         result.add(properties);  
      }  
      if (logger.isDebugEnabled()) {  
         logger.debug("Loaded " + result.size() + " index(es)");  
      }  
      int totalCount = result.stream().mapToInt(Properties::size).sum();  
      return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);  
   }  
   catch (IOException ex) {  
      throw new IllegalStateException("Unable to load indexes from location [" +  
            COMPONENTS_RESOURCE_LOCATION + "]", ex);  
   }  
}

META-INF/spring.components文件格式如下:

全限定类名1=注解全限定类名1,注解全限定类名2
全限定类名2=注解全限定类名1

需要特别注意的是,开启spring.components还需要includeFilters中仅支持@Indexed及其子注解的条件过滤器,如果初始化了componentsIndex,后续扫描时只会在META-INF/spring.components文件中筛选路径匹配的类进行注册。如果遇到包下的bean扫描不到时,可以从这方面考虑。

3.2 扫描

3.2.1 scan

通过ClassPathBeanDefinitionScanner#scan()方法可以扫描指定包,注册XxxProcessor,并且计算本次注册bean的数量:

public int scan(String... basePackages) {  
   int beanCountAtScanStart = this.registry.getBeanDefinitionCount();  
   // 实际扫描方法
   doScan(basePackages);  
   // 注册XxxProcessor
   if (this.includeAnnotationConfig) {  
      AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);  
   }  
   return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);  
}

3.2.2 doScan

ClassPathBeanDefinitionScanner#doScan()方法是扫描的核心方法,它会扫描指定包,设置BeanDefinition的基本属性,最后注册到registry中:

protected Set<BeanDefinitionHolder> doScan(String... basePackages) {  
   Assert.notEmpty(basePackages, "At least one base package must be specified");  
   Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();  
   // 遍历basePackages
   for (String basePackage : basePackages) {  
      // 从指定basePackage中筛选满足条件的类,解析成BeanDefinition
      Set<BeanDefinition> candidates = findCandidateComponents(basePackage);  
      // 遍历candidates,设置基本属性
      for (BeanDefinition candidate : candidates) {  
         // 设置作用域:读取@Scope属性进行设置
         ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);  
         candidate.setScope(scopeMetadata.getScopeName());  
         // 设置beanName:读取@Component属性,或者根据类名生成
         String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);  
         // 设置AbstractBeanDefinition相关基本属性
         if (candidate instanceof AbstractBeanDefinition) {  
            postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);  
         }  
         // 设置AnnotatedBeanDefinition相关基本属性
         if (candidate instanceof AnnotatedBeanDefinition) {  
            AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);  
         }  
         // 校验是否重复注册&注册到Spring容器
         if (checkCandidate(beanName, candidate)) {  
            BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);  
            definitionHolder =  
                  AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);  
            beanDefinitions.add(definitionHolder);  
            registerBeanDefinition(definitionHolder, this.registry);  
         }  
      }  
   }  
   return beanDefinitions;  
}

findCandidateComponents

ClassPathScanningCandidateComponentProvider#findCandidateComponents()包含扫描包的核心代码。

如果开启了spring.components配置,并且includeFilters中只有@Indexed及其子注解条件(或者注解全限定类名以javax.开头),就会从spring.components中扫描满足条件的类进行解析。

如果没有开启spring.component配置,或者includeFilters不满足条件,就会从类路径中扫描满足条件的类进行解析。

ClassPathScanningCandidateComponentProvider#findCandidateComponents()源码如下:

public Set<BeanDefinition> findCandidateComponents(String basePackage) {  
   // 开启spring.components配置,并且includeFilters过滤器仅支持@Indexed及其子注解条件时:从配置中扫描
   if (this.componentsIndex != null && indexSupportsIncludeFilters()) {  
      return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);  
   }  
   // 从指定包中扫描
   else {  
      return scanCandidateComponents(basePackage);  
   }  
}
扫描spring.components

我们先来看ClassPathScanningCandidateComponentProvider#addCandidateComponentsFromIndex()方法,它会从spring.components中获取满足条件的全限定类名,然后读取对应类文件,最后构建成BeanDefinition对象:

private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsIndex index, String basePackage) {  
   Set<BeanDefinition> candidates = new LinkedHashSet<>();  
   try {  
      // 根据includeFilters中的条件和basePackage从spring.components中获取对应类
      Set<String> types = new HashSet<>();  
      for (TypeFilter filter : this.includeFilters) {  
         // stereotype是注解全限定类名,如org.springframework.stereotype.Component
         String stereotype = extractStereotype(filter);  
         if (stereotype == null) {  
            throw new IllegalArgumentException("Failed to extract stereotype from " + filter);  
         }  
         // 根据basePackage和stereotype,从spring.components中获取对应类
         types.addAll(index.getCandidateTypes(basePackage, stereotype));  
      }  
      // 遍历所有类
      for (String type : types) {  
         // 获取元数据
         MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type);  
         // 校验是否满足excludeFilters和includeFilters条件(注意校验顺序)
         if (isCandidateComponent(metadataReader)) {  
            // 解析成BeanDefinition,设置基本属性
            ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);  
            sbd.setSource(metadataReader.getResource());  
            // 校验BeanDefinition是否满足条件:比如是否可以进行初始化
            if (isCandidateComponent(sbd)) {  
               candidates.add(sbd);  
            }  
         }
      }  
   }  
   catch (IOException ex) {  
      throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);  
   }  
   return candidates;  
}

CandidateComponentsIndex#getCandidateTypes()方法定义了从spring.components扫描的逻辑:

public Set<String> getCandidateTypes(String basePackage, String stereotype) {  
   // 获取符合注解条件的所有候补类
   List<Entry> candidates = this.index.get(stereotype);  
   if (candidates != null) {  
      // 过滤出满足包条件的所有类
      return candidates.parallelStream()  
            .filter(t -> t.match(basePackage))  
            .map(t -> t.type)  
            .collect(Collectors.toSet());  
   }  
   return Collections.emptySet();  
}

SimpleMetadataReaderFactory#getMetadataReader()会使用resourceLoader读取类文件(涉及到Resource相关知识,可以查看Spring的Resource体系介绍),然后解析类文件的注解信息:

public MetadataReader getMetadataReader(String className) throws IOException {  
   try {  
      // 类文件路径:classpath:全限定类名.class
      String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX +  
            ClassUtils.convertClassNameToResourcePath(className) + ClassUtils.CLASS_FILE_SUFFIX;  
      // 获取类文件的ClassPathResource对象
      Resource resource = this.resourceLoader.getResource(resourcePath);  
      // 读取元数据
      return getMetadataReader(resource);  
   }  
   catch (FileNotFoundException ex) {  
   }  
}

SimpleMetadataReaderFactory#getMetadataReader()方法实际上会创建一个SimpleMetadataReader对象,在它的构造函数中会解析注解元数据。解析过程会使用ASM操作Java字节码,这里不进行过多介绍(还没研究过):

SimpleMetadataReader(Resource resource, @Nullable ClassLoader classLoader) throws IOException {  
   SimpleAnnotationMetadataReadingVisitor visitor = new SimpleAnnotationMetadataReadingVisitor(classLoader);  
   // 解析注解元数据:调用ClassReader#accept()方法
   getClassReader(resource).accept(visitor, PARSING_OPTIONS);  
   this.resource = resource;  
   this.annotationMetadata = visitor.getMetadata();  
}

回到ClassPathScanningCandidateComponentProvider#addCandidateComponentsFromIndex()方法,接下来会调用ClassPathScanningCandidateComponentProvider#isCandidateComponent()方法进行过滤:

  1. excludeFilters条件过滤(默认是空)。
  2. includeFilters条件过滤(默认是@Component)。
  3. @Conditional条件过滤(参考深入理解AnnotatedBeanDefinitionReader)。

ClassPathScanningCandidateComponentProvider#isCandidateComponent()方法如下:

protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {  
   // 按excludeFilters条件过滤(默认是空)
   for (TypeFilter tf : this.excludeFilters) {  
      if (tf.match(metadataReader, getMetadataReaderFactory())) {  
         return false;  
      }  
   }  
   // 按includeFilters条件过滤(默认是@Component)
   for (TypeFilter tf : this.includeFilters) {  
      if (tf.match(metadataReader, getMetadataReaderFactory())) {  
         // 按@Conditional条件过滤
         return isConditionMatch(metadataReader);  
      }  
   }  
   return false;  
}

接下来,会创建ScannedGenericBeanDefinition对象,设置好相关基本信息。然后还会对ScannedGenericBeanDefinition对象进行一次判断,这次主要是校验这个bean能否成功初始化:

protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {  
   AnnotationMetadata metadata = beanDefinition.getMetadata();  
   return (metadata.isIndependent() && (metadata.isConcrete() ||  
         (metadata.isAbstract() && metadata.hasAnnotatedMethods(Lookup.class.getName()))));  
}
扫描类路径

ClassPathScanningCandidateComponentProvider#scanCandidateComponents()方法会扫描类路径下所有满足条件的类。它的操作与扫描spring.components配置一致,只是类文件来源不同:

private Set<BeanDefinition> scanCandidateComponents(String basePackage) {  
   Set<BeanDefinition> candidates = new LinkedHashSet<>();  
   try {  
      // 类文件路径匹配规则:classpath*:包路径/**/*.class
      String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +  
            resolveBasePackage(basePackage) + '/' + this.resourcePattern;  
      // 加载包路径下所有类文件
      Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);  
      // 遍历所有类文件
      for (Resource resource : resources) {   
         try {  
            // 读取元数据
            MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);  
            // 校验是否满足excludeFilters和includeFilters条件(注意校验顺序)
            if (isCandidateComponent(metadataReader)) {  
               // 解析成BeanDefinition,设置基本属性
               ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);  
               sbd.setSource(resource);  
               // 校验BeanDefinition是否满足条件:比如是否可以进行初始化
               if (isCandidateComponent(sbd)) {  
                  candidates.add(sbd);  
               }  
            }  
         }
         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;  
}

注册Beandefinition

回到ClassPathBeanDefinitionScanner#doScan()方法,此时我们已经得到了BeanDefinition对象。接下来会进行设置作用域、beanName等基本属性,可以参考深入理解AnnotatedBeanDefinitionReader

3.2.3 registerAnnotationConfigProcessors

回到ClassPathBeanDefinitionScanner#scan()方法,在扫描并注册完指定路径下的类后,还会注册XxxProcessor

// 默认为true
if (this.includeAnnotationConfig) {  
   AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);  
}

AnnotationConfigUtils#registerAnnotationConfigProcessors()方法中,会添加许多后处理器:

  • ConfigurationClassPostProcessor:处理@Configuration
  • AutowiredAnnotationBeanPostProcessor:处理@Autowired@Value
  • CommonAnnotationBeanPostProcessor:提供对JSR-250的支持。
  • internalPersistenceAnnotationProcessor:提供对JPA的支持。
  • EventListenerMethodProcessorDefaultEventListenerFactory:处理@EventListener

AnnotationConfigUtils#registerAnnotationConfigProcessors()方法如下:

public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(  
      BeanDefinitionRegistry registry, @Nullable Object source) {  
  
   DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);  
   if (beanFactory != null) {  
      // 添加对@Order的支持
      if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {  
         beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);  
      }  
      // 添加对@Qualifier和@Value的支持
      if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {  
         beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());  
      }  
   }  
   Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);  
   // 添加ConfigurationClassPostProcessor后处理器:@Configuration
   if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));  
   }  
   // 添加AutowiredAnnotationBeanPostProcessor后处理器:@Autowired和@Value
   if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));  
   }  
   // 添加CommonAnnotationBeanPostProcessor后处理器:@PostConstruct、@PreDestroy、@Resource等
   if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));  
   }  
   // 添加PersistenceAnnotationBeanPostProcessor后处理器:支持JPA功能
   if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition();  
      try {  
         def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,  
               AnnotationConfigUtils.class.getClassLoader()));  
      }  
      catch (ClassNotFoundException ex) {  
         throw new IllegalStateException(  
               "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);  
      }  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));  
   }  
   // 添加EventListenerMethodProcessor后处理器:@EventListener
   if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));  
   }  
   // 添加DefaultEventListenerFactory后处理器:@EventListener
   if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {  
      RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);  
      def.setSource(source);  
      beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));  
   }  
  
   return beanDefs;  
}

关于后处理器的执行原理,会在后续的文章中详细介绍。

4 典型案例

4.1 DefaultListableBeanFactory

在使用DefaultListableBeanFactory时,可以用ClassPathBeanDefinitionScanner为其注册bean,这也是最基础的用法:

// 创建registry  
BeanDefinitionRegistry registry = new DefaultListableBeanFactory();  
// 创建scanner,设置registry  
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);  
// 扫描指定包下的bean  
scanner.scan("com.example.component");  
// 获取bean  
BeanFactory beanFactory = (BeanFactory) registry;  
Object bean = beanFactory.getBean("");

4.2 AnnotationConfigApplicationContext

在使用AnnotationConfigApplicationContext时,它内部会调用ClassPathBeanDefinitionScanner成员变量进行注册bean

ApplicationContext context = new AnnotationConfigApplicationContext("com.example.component");

其他AnnotationConfigApplicationContextAnnotationConfigWebApplicationContextAnnotationConfigServletWebApplicationContextAnnotationConfigReactiveWebServerApplicationContextApplicationContext实现类也会使用这种方式进行扫描。

4.3 @ComponentScan

@ComponentScan底层会调用ClassPathBeanDefinitionScanner进行注册beanComponentScanAnnotationParser#parse()

public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {  
   ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,  
         componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);  
  
   Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");  
   boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);  
   scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :  
         BeanUtils.instantiateClass(generatorClass));  
  
   ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");  
   if (scopedProxyMode != ScopedProxyMode.DEFAULT) {  
      scanner.setScopedProxyMode(scopedProxyMode);  
   }  
   else {  
      Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");  
      scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));  
   }  
  
   scanner.setResourcePattern(componentScan.getString("resourcePattern"));  
  
   for (AnnotationAttributes includeFilterAttributes : componentScan.getAnnotationArray("includeFilters")) {  
      List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(includeFilterAttributes, this.environment,  
            this.resourceLoader, this.registry);  
      for (TypeFilter typeFilter : typeFilters) {  
         scanner.addIncludeFilter(typeFilter);  
      }  
   }  
   for (AnnotationAttributes excludeFilterAttributes : componentScan.getAnnotationArray("excludeFilters")) {  
      List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(excludeFilterAttributes, this.environment,  
         this.resourceLoader, this.registry);  
      for (TypeFilter typeFilter : typeFilters) {  
         scanner.addExcludeFilter(typeFilter);  
      }  
   }  
  
   boolean lazyInit = componentScan.getBoolean("lazyInit");  
   if (lazyInit) {  
      scanner.getBeanDefinitionDefaults().setLazyInit(true);  
   }  
  
   Set<String> basePackages = new LinkedHashSet<>();  
   String[] basePackagesArray = componentScan.getStringArray("basePackages");  
   for (String pkg : basePackagesArray) {  
      String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),  
            ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);  
      Collections.addAll(basePackages, tokenized);  
   }  
   for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {  
      basePackages.add(ClassUtils.getPackageName(clazz));  
   }  
   if (basePackages.isEmpty()) {  
      basePackages.add(ClassUtils.getPackageName(declaringClass));  
   }  
  
   scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {  
      @Override  
      protected boolean matchClassName(String className) {  
         return declaringClass.equals(className);  
      }  
   });  
   return scanner.doScan(StringUtils.toStringArray(basePackages));  
}

4.4 context:component-scan/

<context:component-scan/>也会使用ClassPathBeanDefinitionScanner进行注册beanComponentScanBeanDefinitionParser#parse()

public BeanDefinition parse(Element element, ParserContext parserContext) {  
   String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);  
   basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);  
   String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,  
         ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);  
  
   // Actually scan for bean definitions and register them.  
   ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);  
   Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);  
   registerComponents(parserContext.getReaderContext(), beanDefinitions, element);  
  
   return null;  
}

4.5 ClassPathMapperScanner

Mybatis的ClassPathMapperScanner继承了ClassPathBeanDefinitionScanner@MapperScan使用它来注册bean

`org.mybatis.spring.mapper.MapperScannerConfigurer#postProcessBeanDefinitionRegistry():

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {  
  if (this.processPropertyPlaceHolders) {  
    processPropertyPlaceHolders();  
  }  
  
  ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);  
  scanner.setAddToConfig(this.addToConfig);  
  scanner.setAnnotationClass(this.annotationClass);  
  scanner.setMarkerInterface(this.markerInterface);  
  scanner.setSqlSessionFactory(this.sqlSessionFactory);  
  scanner.setSqlSessionTemplate(this.sqlSessionTemplate);  
  scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);  
  scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);  
  scanner.setResourceLoader(this.applicationContext);  
  scanner.setBeanNameGenerator(this.nameGenerator);  
  scanner.setMapperFactoryBeanClass(this.mapperFactoryBeanClass);  
  if (StringUtils.hasText(lazyInitialization)) {  
    scanner.setLazyInitialization(Boolean.valueOf(lazyInitialization));  
  }  
  if (StringUtils.hasText(defaultScope)) {  
    scanner.setDefaultScope(defaultScope);  
  }  
  scanner.registerFilters();  
  scanner.scan(  
      StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));  
}
posted @ 2023-01-14 14:39  Xianuii  阅读(1162)  评论(0编辑  收藏  举报