• spring的compentScan注解扫描类机制


     1主要通过ConfigurationClassParser类的doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)方法来扫描被ComponentScan注解的类。

    2 此方法在spring容器的切入点为ConfigurationClassPostProcessor类,此类实现了接口BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor,所以在spring启动时,会调用该类来插入配置扫描制定包下面的类,生成BeanDefinition。

    3 真正处理扫描类为org.springframework.context.annotation.ClassPathBeanDefinitionScanner,该类的核心方法是:protected Set<BeanDefinitionHolder> doScan(String... basePackages),

      该方法把扫描制定包到的BeanDefinition,然后注入到 BeanDefinitionRegistry中

    	/**
    	 * Perform a scan within the specified base packages,
    	 * returning the registered bean definitions.
    	 * <p>This method does <i>not</i> register an annotation config processor
    	 * but rather leaves this up to the caller.
    	 * @param basePackages the packages to check for annotated classes
    	 * @return set of beans registered if any for tooling registration purposes (never {@code null})
    	 */
    	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) {
    			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;
    	}
    
  • 相关阅读:
    Kubernetes实战(第二版)--第六章 管理Pod容器的生命周期
    Kubernetes实战(第二版)--第五章 在Pods中运行应用程序
    Kubernetes实战(第二版)--第四章 介绍kubernetes API对象
    第三章 部署第一个应用程序
    Kubernetes实战(第二版)----第2章 理解容器
    dfs 例子演示
    Leetcode 135. 分发糖果
    mysql materialized_MySql优化- 执行计划解读与优化(二)
    说说Golang的使用心得
    最详细之教你Jenkins+github自动化部署.Net Core程序到Docker
  • 原文地址:https://www.cnblogs.com/swave/p/10494383.html
Copyright © 2020-2023  润新知