1. 程式人生 > 其它 >Spring原始碼閱讀 - @ComponentScan 處理

Spring原始碼閱讀 - @ComponentScan 處理

1. 概述

配置類是 ConfigurationClassPostProcessor 這個 BeanFactoryPostProcessor(BeanDefinitionRefistryPostProcessor)處理的,內部實際是使用了一個 ConfigurationClassParser進行具體的邏輯實現/處理。

程式碼從 org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass 開始分析

2. @ComponentScan

略去大部分,僅註釋了最常見的。
注意,是可以多個 @ComponentScan 在同一個類上的

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    @AliasFor("basePackages")
    String[] value() default {};

    // 要掃描的包及其子包, 若要型別安全(避免寫錯包), 可以使用 basePackageClasses
    @AliasFor("value")
    String[] basePackages() default {};

    // basePackages 的替代. 可考慮在每個 basePackage 中建立一個特殊的標記類或介面, 用於給這個屬性引用
    Class<?>[] basePackageClasses() default {};
    
    // 被掃描出來的類的 NameGenerator
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    // 略
    Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;

    // 略
    ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;

    // 略
    String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;

    // 略
    boolean useDefaultFilters() default true;

    // 略
    Filter[] includeFilters() default {};

    // 略
    Filter[] excludeFilters() default {};

    // 略
    boolean lazyInit() default false;


    // 略
    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    @interface Filter {

        // 略
        FilterType type() default FilterType.ANNOTATION;

        // 略
        @AliasFor("classes")
        Class<?>[] value() default {};

        // 略
        @AliasFor("value")
        Class<?>[] classes() default {};

        // 略
        String[] pattern() default {};

    }

}

3. 解析流程

// Process any @ComponentScan annotations
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
        sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
// REGISTER_BEAN 階段,那麼和 PARSE_CONFIGURATION 階段的不同?
if (!componentScans.isEmpty() &&
        !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
    for (AnnotationAttributes componentScan : componentScans) {
        // The config class is annotated with @ComponentScan -> perform the scan immediately
        // 指定包掃描,掃描包及其子包,實際最後還是使用 ClassPathBeanDefinitionScanner 來進行掃描的,MyBatis 掃描介面好像也是使用它掃描的
        //    ClassPathBeanDefinitionScanner 掃描到後會先立即注入BD,也就是這裡返回的都是已經注入的BD
        // 這裡具體掃描邏輯放入了 ComponentScanParser,掃描到的 BeanDefinition 也是內部注入的
        // 這裡傳入了一個 getClassName, 是當僅使用 @ComponentScan 但沒有任何配置時預設掃描當前包及其子包
        Set<BeanDefinitionHolder> scannedBeanDefinitions =
                this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
        // Check the set of scanned definitions for any further config classes and parse recursively if needed
        // 配置類則還需要繼續解析, 那麼哪些掃描出來的 BD 不會被當做配置類呢?
        for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
            BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
            if (bdCand == null) {
                bdCand = holder.getBeanDefinition();
            }
            // 是否是配置類,掃描出來的這些需要判斷是否為配置類,進入最初的配置類解析流程
            if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
                parse(bdCand.getBeanClassName(), holder.getBeanName());
            }
        }
    }
}

具體是在 ComponentScanAnnotationParser 中解析的

public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {
    // 誒, 這裡就使用到了 AnnotationConfigApplicationContext 中的 scanner, 類路徑掃描器, 注意, 每次進來生成一個新的例項進行處理
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
            componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);

    // 接著就是根據 @ComponentScan 的配置對掃描器進行配置, 暫時略過這些配置
    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);
    }

    // 這裡是生成要掃描的包的
    // 首先是 basePackages 配置的
    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);
    }
    // 再就是 basePackageClasses 配置的
    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);
        }
    });
    // 掃描, 可也以看出, BD 是否注入到容器是在掃描器中進行處理的
    return scanner.doScan(StringUtils.toStringArray(basePackages));
}

上面只是對 @ComponentScan 進行了進一步讀取處理,下面才是真正的處理邏輯,使用的是 ClassPathBeanDefinitionScanner,這個類在 AnnotationConfigApplicationContext 中也持有一個例項。
下面也略去大部分註釋,僅關注主要邏輯
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan

protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Assert.notEmpty(basePackages, "At least one base package must be specified");
    Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
    for (String basePackage : basePackages) {
        // 這裡就掃描出來所有的 BD,並解析、註冊,也即過濾過程在內部戶已經處理了
        Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
        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);
            }
            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;
}

下面是掃描了篩選的流程

public Set<BeanDefinition> findCandidateComponents(String basePackage) {
	// 略
	if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
		return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
	}
	else {
		// 看這裡
		return scanCandidateComponents(basePackage);
	}
}

private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
    Set<BeanDefinition> candidates = new LinkedHashSet<>();
    try {
        // 拼接, 加上了 "classpath*:" 字首和 "**/*.class" 字尾, 大致可以理解為僅掃描當前類路徑下的,且為當前包及其子包下的 class 檔案
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                resolveBasePackage(basePackage) + '/' + this.resourcePattern;
        // 獲取到所有 class 檔案
        Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
        boolean traceEnabled = logger.isTraceEnabled();
        boolean debugEnabled = logger.isDebugEnabled();
        // 遍歷 class
        for (Resource resource : resources) {
            if (traceEnabled) {
                logger.trace("Scanning " + resource);
            }
            try {
                MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
                // 是否滿足條件,這裡是 Filter 篩選
                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;
}

4. 待續

上面僅將大體流程描述了,很多細節沒有寫,後續再補充細節