博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ConfigurationClassPostProcessor源码
阅读量:2155 次
发布时间:2019-05-01

本文共 4297 字,大约阅读时间需要 14 分钟。

类图

 

负责解析处理所有@Configuration标签类,主要流程在ConfigurationClassParser中进行

 

postProcessBeanDefinitionRegistry

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {    processConfigBeanDefinitions(registry);}

 

processConfigBeanDefinitions

处理full和lite

先得到Spring内置的Bean定义和AnnotationConfigApplicationContext传进来的@Configuration

String[] candidateNames = registry.getBeanDefinitionNames(); // 目前都是spring内置的bean定义和 AnnotationConfigApplicationContext构造传进来的annotatedClassesfor (String beanName : candidateNames) {	BeanDefinition beanDef = registry.getBeanDefinition(beanName);	if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||			ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { // 已经处理过了 full 、lite 已经标记打进去了		if (logger.isDebugEnabled()) {			logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);		}	}	else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { // 这里会处理full 、lite  如果2个都不是 返回false 有1个则返回true		configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));	}}
这里最终得到的configCandidates,就是传进来的annotatedClasses

 

循环遍历解析configCandidates

// ConfigurationClassParserpublic void parse(Set
configCandidates) { for (BeanDefinitionHolder holder : configCandidates) { // 循环遍历configCandidates BeanDefinition bd = holder.getBeanDefinition(); try { if (bd instanceof AnnotatedBeanDefinition) { parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName()); // 注解的进入这里 } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) { parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName()); } else { parse(bd.getBeanClassName(), holder.getBeanName()); } } } this.deferredImportSelectorHandler.process();}

 

 

 

 

checkConfigurationClassCandidate

// 如果是@Configuration 会设置fullif (isFullConfigurationCandidate(metadata)) {	beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);}else if (isLiteConfigurationCandidate(metadata)) {	beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);}else {	return false;}// It's a full or lite configuration candidate... Let's determine the order value, if any.Integer order = getOrder(metadata);if (order != null) {	beanDef.setAttribute(ORDER_ATTRIBUTE, order);}return true;

 

 

 

 

 

postProcessBeanFactory

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {		int factoryId = System.identityHashCode(beanFactory);	if (this.factoriesPostProcessed.contains(factoryId)) {		throw new IllegalStateException(				"postProcessBeanFactory already called on this post-processor against " + beanFactory);	}	this.factoriesPostProcessed.add(factoryId);	if (!this.registriesPostProcessed.contains(factoryId)) {		// BeanDefinitionRegistryPostProcessor hook apparently not supported...		// Simply call processConfigurationClasses lazily at this point then.		processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);	}	enhanceConfigurationClasses(beanFactory); //对标记@Configuration注解的类 会cblig增强	beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory)); // 添加ImportAwareBeanPostProcessor}

isFullConfigurationClass

public static boolean isFullConfigurationClass(BeanDefinition beanDef) {		return CONFIGURATION_CLASS_FULL.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE));}

 

ConfigurationClassUtils

判断是否有Configuration注解

public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {	return metadata.isAnnotated(Configuration.class.getName());}

如果存在Configuration,则设置CONFIGURATION_CLASS_ATTRIBUTE属性为full

if (isFullConfigurationCandidate(metadata)) {	beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);}
public void setAttribute(String name, @Nullable Object value) {	super.setAttribute(name, new BeanMetadataAttribute(name, value));}

 

isLiteConfigurationCandidate

会判断是否存在下面4个注解中的1个,存在,则返回true

candidateIndicators.add(Component.class.getName());candidateIndicators.add(ComponentScan.class.getName());candidateIndicators.add(Import.class.getName());candidateIndicators.add(ImportResource.class.getName());

如果上面4个注解,都不存在,则判断是否存在Bean注解

return metadata.hasAnnotatedMethods(Bean.class.getName());

也就是存在上面5个中的1个,就返回true,否则返回false

 

转载地址:http://lvxwb.baihongyu.com/

你可能感兴趣的文章
什么是 Q-learning
查看>>
用一个小游戏入门深度强化学习
查看>>
如何应用 BERT :Bidirectional Encoder Representations from Transformers
查看>>
5 分钟入门 Google 最强NLP模型:BERT
查看>>
强化学习第1课:像学自行车一样的强化学习
查看>>
强化学习第2课:强化学习,监督式学习,非监督式学习的区别
查看>>
强化学习第3课:有些问题就像个赌局
查看>>
强化学习第4课:这些都可以抽象为一个决策过程
查看>>
强化学习第5课:什么是马尔科夫决策过程
查看>>
强化学习第6课:什么是 Crossentropy 方法
查看>>
强化学习第7课:交叉熵方法的一些局限性
查看>>
强化学习 8: approximate reinforcement learning
查看>>
图解什么是 Transformer
查看>>
代码实例:如何使用 TensorFlow 2.0 Preview
查看>>
6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现
查看>>
走进JavaWeb技术世界1:JavaWeb的由来和基础知识
查看>>
走进JavaWeb技术世界2:JSP与Servlet的曾经与现在
查看>>
走进JavaWeb技术世界3:JDBC的进化与连接池技术
查看>>
走进JavaWeb技术世界4:Servlet 工作原理详解
查看>>
走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
查看>>