zoukankan      html  css  js  c++  java
  • Spring5源码解析5-ConfigurationClassPostProcessor (上)

    接上回,我们讲到了refresh()方法中的invokeBeanFactoryPostProcessors(beanFactory)方法主要在执行BeanFactoryPostProcessor和其子接口BeanDefinitionRegistryPostProcessor的方法。

    在创建AnnotationConfigApplicationContext对象时Spring就添加了一个非常重要的BeanFactoryPostProcessor接口实现类:ConfigurationClassPostProcessor。注意,这里说的添加只是添加到容器的beanDefinitionMap中,还没有创建真正的实例Bean。

    简单回顾一下ConfigurationClassPostProcessor是在什么时候被添加到容器中的:在AnnotationConfigApplicationContext的无参构造器中创建AnnotatedBeanDefinitionReader对象时会向传入的BeanDefinitionRegistry中注册解析注解配置类相关的processors的BeanDefinitionConfigurationClassPostProcessor就是在此处被添加到容器中的。


    ConfigurationClassPostProcessor

    先看一些ConfigurationClassPostProcessor的继承体系:

    ConfigurationClassPostProcessor

    ConfigurationClassPostProcessor实现了BeanDefinitionRegistryPostProcessor接口,也就拥有了在Spring容器启动时,往容器中注册BeanDefinition的能力。

    我们知道,ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry方法是在refresh();方法中的invokeBeanFactoryPostProcessors(beanFactory);中被执行的,下面我们就一起来看一下该方法。

    ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
    	int registryId = System.identityHashCode(registry);
    	if (this.registriesPostProcessed.contains(registryId)) {
    		throw new IllegalStateException(
    				"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
    	}
    	if (this.factoriesPostProcessed.contains(registryId)) {
    		throw new IllegalStateException(
    				"postProcessBeanFactory already called on this post-processor against " + registry);
    	}
    	this.registriesPostProcessed.add(registryId);
    
    	processConfigBeanDefinitions(registry);
    }
    

    主要的逻辑在processConfigBeanDefinitions(registry);中,点开源码:

    public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
    	List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
    	//获取所有的BeanDefinitionName
    	String[] candidateNames = registry.getBeanDefinitionNames();
    
    	for (String beanName : candidateNames) {
    		BeanDefinition beanDef = registry.getBeanDefinition(beanName);
    
    		// https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/core.html#beans-java-basic-concepts
    		// Full @Configuration vs “lite” @Bean mode
    		if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||
    				ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {
    			if (logger.isDebugEnabled()) {
    				logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
    			}
    		}
    
    		// 校验是否为配置类
    		// 配置类分为两种 Full @Configuration vs “lite” @Bean mode
    		// 校验之后在 BeanDefinition 中添加标志属性
    		// 如果满足条件则加入到configCandidates
    		else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
    			// 如果是配置类,就放到 configCandidates 变量中
    			configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
    		}
    	}
    
    	// Return immediately if no @Configuration classes were found
    	if (configCandidates.isEmpty()) {
    		return;
    	}
    
    	// Sort by previously determined @Order value, if applicable
    	configCandidates.sort((bd1, bd2) -> {
    		int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
    		int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
    		return Integer.compare(i1, i2);
    	});
    
    	// Detect any custom bean name generation strategy supplied through the enclosing application context
    	SingletonBeanRegistry sbr = null;
    	// 传入的 registry 是 DefaultListableBeanFactory
    	if (registry instanceof SingletonBeanRegistry) {
    		sbr = (SingletonBeanRegistry) registry;
    		if (!this.localBeanNameGeneratorSet) {
    			//获取自定义BeanNameGenerator,一般情况下为空
    			BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
    			if (generator != null) {
    				this.componentScanBeanNameGenerator = generator;
    				this.importBeanNameGenerator = generator;
    			}
    		}
    	}
    
    	if (this.environment == null) {
    		this.environment = new StandardEnvironment();
    	}
    
    	// Parse each @Configuration class
    	// new ConfigurationClassParser,用来解析 @Configuration 类
    	ConfigurationClassParser parser = new ConfigurationClassParser(
    			this.metadataReaderFactory, this.problemReporter, this.environment,
    			this.resourceLoader, this.componentScanBeanNameGenerator, registry);
    
    	// 将 configCandidates 转成 set  candidates , 去重
    	Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
    	Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
    	do {
    		// 解析配置类
    		parser.parse(candidates);
    		parser.validate();
    
    		Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
    		configClasses.removeAll(alreadyParsed);
    
    		// Read the model and create bean definitions based on its content
    		if (this.reader == null) {
    			this.reader = new ConfigurationClassBeanDefinitionReader(
    					registry, this.sourceExtractor, this.resourceLoader, this.environment,
    					this.importBeanNameGenerator, parser.getImportRegistry());
    		}
    		// Import类,@Bean,@ImportResource 转化为 BeanDefinition
    		this.reader.loadBeanDefinitions(configClasses);
    		alreadyParsed.addAll(configClasses);
    
    		candidates.clear();
    		// 再获取一下容器中BeanDefinition的数据,如果发现数量增加了,说明有新的BeanDefinition被注册了
    		if (registry.getBeanDefinitionCount() > candidateNames.length) {
    			String[] newCandidateNames = registry.getBeanDefinitionNames();
    			Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
    			Set<String> alreadyParsedClasses = new HashSet<>();
    			for (ConfigurationClass configurationClass : alreadyParsed) {
    				alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
    			}
    			for (String candidateName : newCandidateNames) {
    				if (!oldCandidateNames.contains(candidateName)) {
    					BeanDefinition bd = registry.getBeanDefinition(candidateName);
    					if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
    							!alreadyParsedClasses.contains(bd.getBeanClassName())) {
    						candidates.add(new BeanDefinitionHolder(bd, candidateName));
    					}
    				}
    			}
    			candidateNames = newCandidateNames;
    		}
    	}
    	while (!candidates.isEmpty());
    
    	// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
    	if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
    		sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
    	}
    
    	if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
    		// Clear cache in externally provided MetadataReaderFactory; this is a no-op
    		// for a shared cache since it'll be cleared by the ApplicationContext.
    		((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
    	}
    }
    

    获取所有的BeanDefinitionNames,然后循环这个数组,判断其是否为配置类。

    candidateNames

    前5个是Spring注册的内置processor,最后一个是传入给AnnotationConfigApplicationContext的配置类AppConfig.class

    在Spring中存在两种ConfigurationClass,一种是FullConfigurationClass另一种是LiteConfigurationClass。关于这两者的区别,可以参看笔者文章之前关于Full @Configuration 和 lite @Bean mode的文章。

    ConfigurationClassUtils#checkConfigurationClassCandidate方法内部就是在判断属于哪种配置类,并在BeanDefinition中标记判断结果。其具体的判断逻辑如下:

    public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {
    	return metadata.isAnnotated(Configuration.class.getName());
    }
    private static final Set<String> candidateIndicators = new HashSet<>(8);
    
    static {
    	candidateIndicators.add(Component.class.getName());
    	candidateIndicators.add(ComponentScan.class.getName());
    	candidateIndicators.add(Import.class.getName());
    	candidateIndicators.add(ImportResource.class.getName());
    }
    
    public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
    	// Do not consider an interface or an annotation...
    	if (metadata.isInterface()) {
    		return false;
    	}
    
    	// Any of the typical annotations found?
    	for (String indicator : candidateIndicators) {
    		if (metadata.isAnnotated(indicator)) {
    			return true;
    		}
    	}
    
    	// Finally, let's look for @Bean methods...
    	try {
    		return metadata.hasAnnotatedMethods(Bean.class.getName());
    	} catch (Throwable ex) {
    		if (logger.isDebugEnabled()) {
    			logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]: " + ex);
    		}
    		return false;
    	}
    }
    

    判断configCandidates变量中存放的

    配置类是否为空,如果不为空,则对其进行排序。

    configCandidates

    创建ConfigurationClassParser对象,用于解析@Configuration类,完成包的扫描、BeanDefinition的注册。主要通过执行parser.parse(candidates);方法来完成。

    执行parser.parse(candidates)方法前 :

    执行parser.parse(candidates)方法前

    执行parser.parse(candidates)方法后 :

    执行parser.parse(candidates)方法后

    解析完配置类之后,紧接着又执行了this.reader.loadBeanDefinitions(configClasses);方法。这个方法主要是用来处理Import类@Bean@ImportResource注解。关于这两个方法的具体细节,我们下次再讲。

    最后又加了入了对ImportAware接口支持所需要的Bean。

    // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
    if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
    	sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
    }
    

    关于对ImportAware接口的使用,我们也下次再讲。


    未完待续......

    源码学习笔记:https://github.com/shenjianeng/spring-code-study

    欢迎各位关注公众号,大家一起学习成长。
    Coder小黑

  • 相关阅读:
    redis 系列27 Cluster高可用 (2)
    redis 系列26 Cluster高可用 (1)
    redis 系列25 哨兵Sentinel (高可用演示 下)
    redis 系列24 哨兵Sentinel (中)
    redis 系列23 哨兵Sentinel (上)
    (网页)jQuery判断checkbox是否选中的方法
    (后端)swagger
    (其他)2018下半年目标
    (后端)Sql Server日期查询-SQL查询今天、昨天、7天内、30天(转)
    (网页)HTML中INPUT type="date"标签如何赋值注意问题(转)
  • 原文地址:https://www.cnblogs.com/coderxiaohei/p/11685144.html
Copyright © 2011-2022 走看看