• Spring的@Bean注解原理详解


    一. @Bean注解的作用

    这是一个我们很常用的注解,作用是指示一个方法生成一个由Spring管理的Bean。
    
    • 1

    之前的文章都是使用的xml或者自定义形式的项目研究的,本篇是讲注解的,所以直接使用最简单的SpringBoot项目了,版本号:2.3.12.RELEASE
    在这里插入图片描述
    在这里插入图片描述
    本篇就已这个例子进行分析@Bean注解的实现方式

    二. 先了解BeanFactoryPostProcessor

    BeanFactoryPostProcessorBeanPostProcessor是不是一样的,关于BeanPostProcessor可以看这篇:Spring的BeanPostProcessor分析,千万别把两个搞混了。

    BeanFactoryPostProcessor源码:

    @FunctionalInterface
    public interface BeanFactoryPostProcessor {
    	/**
    	 * 在标准初始化之后修改应用程序上下文的内部 bean 工厂。所有 bean 定义都将被加载,但还没有 bean 被实例化。
    	 * 这允许覆盖或添加属性,甚至是急切初始化的 bean。
    	 */
    	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它或者实现bean动态代理等。同时可以定义多个BeanFactoryPostProcessor,通过设置’order’属性来确定各个BeanFactoryPostProcessor执行顺序。

    与BeanFactoryPostProcessor相关的两个很重要的类:

    • BeanDefinitionRegistryPostProcessor 这个接口是对BeanFactoryPostProcessor的进一步扩展,其中的postProcessBeanDefinitionRegistry方法可以对BeanDefinition做更多的定义
    • ConfigurationClassPostProcessor 这个类实现了BeanDefinitionRegistryPostProcessor,具体的实现了对Bean的扫描和BeanDefinition的修改

    三. 源码分析

    @Bean注解是在的org.springframework.context.support.AbstractApplicationContext#refresh方法中被加载的,具体是在org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors方法中被扫描到的,关于refresh方法就不再做过多描述了,不了解的同学可以看Spring栏目里面的文章进行了解。
    首先来看refresh方法中的invokeBeanFactoryPostProcessors这一步:

    // Invoke factory processors registered as beans in the context.
    invokeBeanFactoryPostProcessors(beanFactory);
    
    • 1
    • 2

    这个方法主要作用就是根据反射机制从BeanDefinitionRegistry(bean定义注册中心)中找到所有实现了BeanFactoryPostProcessor接口bean,并调用其postProcessBeanFactory()接口方法, 其实就是对Bean定义的增强/修改,同时也是一个非常有效的扩展点。跟进这个方法:

    public static void invokeBeanFactoryPostProcessors(
    			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    	//一开始进来的时候,beanFactoryPostProcessors中有三个BeanFactoryPostProcessor的实现,这三个与本篇关系不大就不详细说了,感兴趣的同学可以自行断点查看
    	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
    	// 如果有的话,首先执行 BeanDefinitionRegistryPostProcessors
    	Set<String> processedBeans = new HashSet<>();
    
    	if (beanFactory instanceof BeanDefinitionRegistry) {
    		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    		List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
    		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
    
    		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
    			//如果入参的BeanFactoryPostProcessor 中有实现自BeanDefinitionRegistryPostProcessor的
    			// 那就先执行它的postProcessBeanDefinitionRegistry  然后对beanFactoryPostProcessors中的进行分组
    			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
    				BeanDefinitionRegistryPostProcessor registryProcessor =
    						(BeanDefinitionRegistryPostProcessor) postProcessor;
    				//
    				registryProcessor.postProcessBeanDefinitionRegistry(registry);
    				registryProcessors.add(registryProcessor);
    			}
    			else {
    				regularPostProcessors.add(postProcessor);
    			}
    		}
    
    		// Do not initialize FactoryBeans here: We need to leave all regular beans
    		// uninitialized to let the bean factory post-processors apply to them!
    		// Separate between BeanDefinitionRegistryPostProcessors that implement
    		// PriorityOrdered, Ordered, and the rest.
    		List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
    
    		// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
    		// 首先,调用实现 PriorityOrdered 的 BeanDefinitionRegistryPostProcessor
    		
    		// 这里是从beanFactory中查找BeanDefinitionRegistryPostProcessor的Bean
    		// 这里获取的结果是:org.springframework.context.annotation.internalConfigurationAnnotationProcessor   能获取到这个的原因在下面写
    		// 对应的类是:org.springframework.context.annotation.ConfigurationClassPostProcessor  这个类也就是本篇的重点类
    		String[] postProcessorNames =
    				beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    		for (String ppName : postProcessorNames) {
    			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
    				// ConfigurationClassPostProcessor实现了PriorityOrdered  所以优先处理  此处先实例化然后用于后续执行
    				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    				// 记录已实例化的  其实就是记录一下已经处理过的  后续需要排除
    				processedBeans.add(ppName);
    			}
    		}
    		sortPostProcessors(currentRegistryProcessors, beanFactory);
    		registryProcessors.addAll(currentRegistryProcessors);
    		// 然后进入这个方法处理  这一步就是具体的ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry执行逻辑了
    		// 下面会详细分析  这里打个标记!!!!!!  方便搜索
    		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    		currentRegistryProcessors.clear();
    
    		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
    		// 接下来,调用实现 Ordered 的 BeanDefinitionRegistryPostProcessors
    		postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    		for (String ppName : postProcessorNames) {
    			if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
    				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    				processedBeans.add(ppName);
    			}
    		}
    		sortPostProcessors(currentRegistryProcessors, beanFactory);
    		registryProcessors.addAll(currentRegistryProcessors);
    		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    		currentRegistryProcessors.clear();
    
    		// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
    		// 最后,循环调用所有其他BeanDefinitionRegistryPostProcessors直到没有未处理的
    		boolean reiterate = true;
    		while (reiterate) {
    			reiterate = false;
    			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    			for (String ppName : postProcessorNames) {
    				if (!processedBeans.contains(ppName)) {
    					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    					processedBeans.add(ppName);
    					reiterate = true;
    				}
    			}
    			sortPostProcessors(currentRegistryProcessors, beanFactory);
    			registryProcessors.addAll(currentRegistryProcessors);
    			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    			currentRegistryProcessors.clear();
    		}
    		// 到这里 我们可以看到BeanDefinitionRegistryPostProcessor处理流程是先处理PriorityOrdered的
    		// 再处理Ordered的, 最后在循环处理所有的
    		// 目的是先执行内置必要的BeanDefinitionRegistryPostProcessor,在处理过程中可能会有添加新的BeanDefinitionRegistryPostProcessor实现,
    		// 然后重新获取,排除已执行的然后根据Ordered排序再执行实现了Ordered的
    		// 上面两个都执行完成之后,不可避免的还会有其他的BeanDefinitionRegistryPostProcessor实现
    		// 最后就再把剩余的BeanDefinitionRegistryPostProcessor实现再循环处理
    		// 循环的原因也是因为在执行的过程中可能会有新的BeanDefinitionRegistryPostProcessor实现添加,所以循环执行所有的
    		
    		// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
    		// 到这里再调用到目前为止处理的所有处理器的 postProcessBeanFactory 回调  也就是本方法入参beanFactoryPostProcessors进来的那几个
    		// 其实就是BeanFactoryPostProcessor的postProcessBeanFactory
    		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
    		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    	}
    
    	else {
    		// Invoke factory processors registered with the context instance.
    		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    	}
    
    	// Do not initialize FactoryBeans here: We need to leave all regular beans
    	// uninitialized to let the bean factory post-processors apply to them!
    	// 不要在此处初始化 FactoryBeans:我们需要让所有常规 bean 保持未初始化状态,以便 bean 工厂后处理器应用到它们!
    
    	// 从工厂中查找BeanFactoryPostProcessor的实现
    	String[] postProcessorNames =
    			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    
    	// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    	// Ordered, and the rest.
    	// 将实现 PriorityOrdered、Ordered、其余部分 的 BeanFactoryPostProcessor 分组
    	List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    	List<String> orderedPostProcessorNames = new ArrayList<>();
    	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    	for (String ppName : postProcessorNames) {
    		if (processedBeans.contains(ppName)) {
    			// 跳过在上面已经执行过的,其实在我们的例子中这里跳过的就是上面执行过的ConfigurationClassPostProcessor
    			// skip - already processed in first phase above
    		}
    		else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
    			priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
    		}
    		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
    			orderedPostProcessorNames.add(ppName);
    		}
    		else {
    			nonOrderedPostProcessorNames.add(ppName);
    		}
    	}
    
    	// 然后再根据分组结果,依次执行BeanFactoryPostProcessor的处理流程
    
    	// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    	// 首先,调用实现 PriorityOrdered 的 BeanFactoryPostProcessor
    	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    
    	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    	// 接下来,调用实现 Ordered 的 BeanFactoryPostProcessors
    	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    	for (String postProcessorName : orderedPostProcessorNames) {
    		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    	}
    	sortPostProcessors(orderedPostProcessors, beanFactory);
    	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    
    	// Finally, invoke all other BeanFactoryPostProcessors.
    	// 最后,调用所有其他 BeanFactoryPostProcessor
    	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    	for (String postProcessorName : nonOrderedPostProcessorNames) {
    		nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    	}
    	invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
    
    	// Clear cached merged bean definitions since the post-processors might have
    	// modified the original metadata, e.g. replacing placeholders in values...
    	beanFactory.clearMetadataCache();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166

    上面这段源码贴出来,并写了一些注释,主要是说明了这个方法的大体执行逻辑,我们本篇分析的是@Bean的原理,所以我们重点看其中的:invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);这一步(源码中注释标记“!!!!!!”的那一步),也就是实现了PriorityOrderedBeanDefinitionRegistryPostProcessor处理。

    在进行重点源码分析前先说一下为什么在beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);这一步能获取到org.springframework.context.annotation.internalConfigurationAnnotationProcessor,原因是SpringBoot在启动时候,org.springframework.boot.SpringApplication#run(java.lang.String...)方法中有一步:context = createApplicationContext();,其中创建了容器:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext,在实例化这个容器的时候,他的无参构造会执行到:org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object)这个方法,这个里面给Spring工厂中注册了org.springframework.context.annotation.internalConfigurationAnnotationProcessor,这一步大家可以找到地方自行断点查看,不是本篇的重点就不详细说了。

    进入invokeBeanDefinitionRegistryPostProcessors方法:

    private static void invokeBeanDefinitionRegistryPostProcessors(
    		Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {
    	// 根据我们上面跟进的内容,这里的postProcessors只有一个值:ConfigurationClassPostProcessor
    	for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
    		postProcessor.postProcessBeanDefinitionRegistry(registry);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    所以我们跟进到org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry看:

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    然后继续跟进到:processConfigBeanDefinitions(registry);

    public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
    	List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
    	// 获取工厂中的BeanDefinitionNames
    	String[] candidateNames = registry.getBeanDefinitionNames();
    
    	//循环所有的BeanNames 找出候选配置类
    	for (String beanName : candidateNames) {
    		BeanDefinition beanDef = registry.getBeanDefinition(beanName);
    		if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {
    			if (logger.isDebugEnabled()) {
    				logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
    			}
    		}
    		else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
    			// 在本例中这里其实就是SpringBoot的启动类   因为@SpringBootApplication注解是个组合注解,其中包含@Configuration注解
    			configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
    		}
    	}
    
    	// Return immediately if no @Configuration classes were found
    	// 如果没有找到 @Configuration 类,则立即返回
    	if (configCandidates.isEmpty()) {
    		return;
    	}
    
    	// Sort by previously determined @Order value, if applicable
    	// 按@Order 值排序(如果合适)
    	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
    	// 检测通过应用程序上下文提供的任何自定义 bean 名称生成策略  默认没有
    	SingletonBeanRegistry sbr = null;
    	if (registry instanceof SingletonBeanRegistry) {
    		sbr = (SingletonBeanRegistry) registry;
    		if (!this.localBeanNameGeneratorSet) {
    			BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(
    					AnnotationConfigUtils.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
    	// 解析每个 @Configuration 类
    	ConfigurationClassParser parser = new ConfigurationClassParser(
    			this.metadataReaderFactory, this.problemReporter, this.environment,
    			this.resourceLoader, this.componentScanBeanNameGenerator, registry);
    
    	Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
    	Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
    	do {
    		// 解析的重点方法  
    		// 这里面会进行包扫描并处理@Bean等注解的解析,并封装成ConfigurationClass,设置对应的标记BeanMethod
    		// 下面会展开分析这个方法  
    		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());
    		}
    		// 到这里已经获取到所有的配置类并封装成ConfigurationClass
    		// 通过ConfigurationClass的BeanMethod解析@Bean注解的相关内容并构造出要注册的对象的BeanDefinition
    		// 最后注册到工厂中去
    		// 这一步下面也会展开解析
    		this.reader.loadBeanDefinitions(configClasses);
    		alreadyParsed.addAll(configClasses);
    
    		candidates.clear();
    		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();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    这个方法大家看下注释,我们重点看下面这两个方法:

    • parser.parse(candidates);
    • this.reader.loadBeanDefinitions(configClasses);
      这两个方法是核心,依次分开解析:

    3.1 parser.parse(candidates)解析

    进入源码:org.springframework.context.annotation.ConfigurationClassParser#parse(java.util.Set)跟进:

    public void parse(Set<BeanDefinitionHolder> configCandidates) {
    	for (BeanDefinitionHolder holder : configCandidates) {
    		BeanDefinition bd = holder.getBeanDefinition();
    		try {
    			if (bd instanceof AnnotatedBeanDefinition) {
    				// 注解类型的BeanDefinition 我们的SpringBoot启动类会进入这个  所以进入这个parse方法
    				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());
    			}
    		}
    		catch (BeanDefinitionStoreException ex) {
    			throw ex;
    		}
    		catch (Throwable ex) {
    			throw new BeanDefinitionStoreException(
    					"Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
    		}
    	}
    
    	this.deferredImportSelectorHandler.process();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    跟进parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());来到:org.springframework.context.annotation.ConfigurationClassParser#processConfigurationClass

    protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {
    	if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
    		return;
    	}
    	// 这里是第一次加载 所以肯定是空的
    	ConfigurationClass existingClass = this.configurationClasses.get(configClass);
    	if (existingClass != null) {
    		if (configClass.isImported()) {
    			if (existingClass.isImported()) {
    				existingClass.mergeImportedBy(configClass);
    			}
    			// Otherwise ignore new imported config class; existing non-imported class overrides it.
    			return;
    		}
    		else {
    			// Explicit bean definition found, probably replacing an import.
    			// Let's remove the old one and go with the new one.
    			this.configurationClasses.remove(configClass);
    			this.knownSuperclasses.values().removeIf(configClass::equals);
    		}
    	}
    
    	// Recursively process the configuration class and its superclass hierarchy.
    	// 递归处理配置类及其超类层次结构
    	// 这一步是做校验并封装成SourceClass 
    	SourceClass sourceClass = asSourceClass(configClass, filter);
    	do {
    		// 循环递归处理 
    		sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);
    	}
    	while (sourceClass != null);
    
    	// 处理完成后加入缓存
    	this.configurationClasses.put(configClass, configClass);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    详细看:doProcessConfigurationClass(configClass, sourceClass, filter);org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass

    protected final SourceClass doProcessConfigurationClass(
    			ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
    			throws IOException {
    
    	// 是不是有Component注解  启动类的@SpringBootApplication注解中有Component注解 所以会进入if
    	if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
    		// Recursively process any member (nested) classes first
    		// 首先递归处理任何成员(嵌套)类   这个类没有嵌套  所以略过
    		processMemberClasses(configClass, sourceClass, filter);
    	}
    
    	// Process any @PropertySource annotations
    	// 处理任何 @PropertySource 注释   没有这个注释所以跳过
    	for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
    			sourceClass.getMetadata(), PropertySources.class,
    			org.springframework.context.annotation.PropertySource.class)) {
    		if (this.environment instanceof ConfigurableEnvironment) {
    			processPropertySource(propertySource);
    		}
    		else {
    			logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
    					"]. Reason: Environment must implement ConfigurableEnvironment");
    		}
    	}
    
    	// Process any @ComponentScan annotations
    	// 处理任何@ComponentScan注释      @SpringBootApplication注解中有@ComponentScan注解 所以会获取到一个AnnotationAttributes
    	Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
    			sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
    	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
    			// 配置类使用 @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
    			// 检查扫描出来的Bean定义,如果需要并循环解析更深层级的配置
    			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());
    				}
    			}
    		}
    	}
    
    	// Process any @Import annotations
    	// 处理任何 @Import 注释
    	processImports(configClass, sourceClass, getImports(sourceClass), filter, true);
    
    	// Process any @ImportResource annotations
    	// 处理任何 @ImportResource 注释
    	AnnotationAttributes importResource =
    			AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
    	if (importResource != null) {
    		String[] resources = importResource.getStringArray("locations");
    		Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
    		for (String resource : resources) {
    			String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
    			configClass.addImportedResource(resolvedResource, readerClass);
    		}
    	}
    
    	// Process individual @Bean methods
    	// 处理单个 @Bean 方法
    	Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
    	// 获取到所有的@Bean方法的元数据
    	for (MethodMetadata methodMetadata : beanMethods) {
    		// 封装成BeanMethod添加到ConfigurationClass
    		configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
    	}
    
    	// Process default methods on interfaces
    	// 处理接口上的默认方法
    	processInterfaces(configClass, sourceClass);
    
    	// Process superclass, if any
    	if (sourceClass.getMetadata().hasSuperClass()) {
    		String superclass = sourceClass.getMetadata().getSuperClassName();
    		if (superclass != null && !superclass.startsWith("java") &&
    				!this.knownSuperclasses.containsKey(superclass)) {
    			this.knownSuperclasses.put(superclass, configClass);
    			// Superclass found, return its annotation metadata and recurse
    			return sourceClass.getSuperClass();
    		}
    	}
    
    	// No superclass -> processing is complete
    	return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    上面这段代码步骤是:

    1. 先处理@Component的嵌套类
    2. 再处理@PropertySource
    3. 再处理@ComponentScan,这一步是包扫描的重点过程,扫描到包路径下的所有符合条件的Bean并封装成BeanDefinitionHolder返回
    4. 再处理@Import
    5. 再处理@ImportResource
    6. 再处理@Bean,将扫描出来的所有@Bean元数据记录下来
    7. 再处理接口上的@Bean
    8. 如果有父类处理父类

    我们重点来看@ComponentScan的这一步,所以跟进this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());

    public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
    	// 创建BeanDefinition的扫描器
    	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
    			componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
    
    	//获取@ComponentScan注解的nameGenerator Class
    	Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
    	boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
    	scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
    			BeanUtils.instantiateClass(generatorClass));
    
    	//获取@ComponentScan注解的scopedProxy
    	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));
    	}
    
    	//获取@ComponentScan注解的resourcePattern
    	scanner.setResourcePattern(componentScan.getString("resourcePattern"));
    
    	//获取@ComponentScan注解的includeFilters
    	for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) {
    		for (TypeFilter typeFilter : typeFiltersFor(filter)) {
    			scanner.addIncludeFilter(typeFilter);
    		}
    	}
    	//获取@ComponentScan注解的excludeFilters
    	for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {
    		for (TypeFilter typeFilter : typeFiltersFor(filter)) {
    			scanner.addExcludeFilter(typeFilter);
    		}
    	}
    
    	//获取@ComponentScan注解的lazyInit
    	boolean lazyInit = componentScan.getBoolean("lazyInit");
    	if (lazyInit) {
    		scanner.getBeanDefinitionDefaults().setLazyInit(true);
    	}
    
    	Set<String> basePackages = new LinkedHashSet<>();
    	// 获取@ComponentScan注解的basePackages  
    	// 获取配置的扫描包路径,这个大家应该用过,有时候会在启动类上配置这个用来自定义包扫描路径
    	// 在我们的例子中是没有配置的  所有这个为空
    	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));
    	}
    	// 如果没有配置basePackages   那么取启动类所在的包路径!!!
    	if (basePackages.isEmpty()) {
    		basePackages.add(ClassUtils.getPackageName(declaringClass));
    	}
    
    	scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
    		@Override
    		protected boolean matchClassName(String className) {
    			return declaringClass.equals(className);
    		}
    	});
    	// 根据获取到的包路径进行扫描  在我们的例子中这里就是启动类所在的包com.ygz.test1
    	return scanner.doScan(StringUtils.toStringArray(basePackages));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    这段代码主要是解析@ComponentScan注解的元数据,并获取到要扫描的包路径,代码不难就不细说了大家看注释就行,我们接着跟进到scanner.doScan(StringUtils.toStringArray(basePackages));

    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) {
    		// 通过包路径进行扫描 把所有扫描出来的Bean封装成BeanDefinition返回出来
    		// 这一步其实就是spring的Bean扫描流程了,本篇主要是@Bean的解析过程,这里就不深入跟进了,里面内容较多  大家可以自行debug查看
    		Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
    		// 这个循环主要是把获取到的BeanDefinition做进一步的处理
    		// 设置scope、设置其他的一些属性、检查是否需要代理需要的话创建代理等  最终把这个BeanDefinition 注册到工厂中
    		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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    到这里就已经扫描到所有的Bean的BeanDefinition 并注册到工厂了,返回所有的BeanDefinition ,然后我们回到org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass方法的解析@ComponentScan代码位置,通过Set scannedBeanDefinitions = this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());这一步拿到了扫描出来的所有BeanDefinition ,然后循环每一个并检查是不是配置类,然后如果是配置类,那么再次调用解析方法解析配置类,这里其实类似递归过程。下面把这部分代码再贴出来一下

    // 这一段是org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass方法解析@ComponentScan注解的部分代码
    
    
    // The config class is annotated with @ComponentScan -> perform the scan immediately
    // 通过@ComponentScan扫描出所有的Bean并获取到BeanDefinitionHolder
    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
    // 循环判断是不是配置类,如果是配置类那么继续递归解析
    for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
    	BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
    	if (bdCand == null) {
    		bdCand = holder.getBeanDefinition();
    	}
    	// 在我们的例子中,TestConfig类标记有@Configuration注解,所以是配置类,会进入这个if
    	// 然后会在parse方法中继续递归的去解析TestConfig类,在解析到@Bean注解的时候就会把TestConfig类的myTest()方法扫描出来
    	// 这里这个递归大家要能理解,这里第一次parse是通过SpringBoot的启动类扫描所有的Bean
    	// 获取到所有的BeanDefinition之后循环每一个,查看是不是配置类,如果是配置类那么递归调用parse方法
    	// 比如在我们的例子中TestConfig被启动类扫描到之后,会在这里再次被parse,然后再次进入本方法(就是doProcessConfigurationClass)
    	// 当再次进入之后,TestConfig中有方法myTest()标记了@Bean注解  那么就会被@Bean注解处理流程处理 这个处理我们下面会跟进
    	// 这里大家要弄清楚这个递归流程  如果看文章看不明白可以debug跟进查看
    	if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
    		parse(bdCand.getBeanClassName(), holder.getBeanName());
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在上面这段代码中ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)这个判断里有一个小细节这里提一下,我们直接看这段代码中的部分(不把代码全贴出来,大家可以自己点进源码看):

    Map<String, Object> config = metadata.getAnnotationAttributes(Configuration.class.getName());
    if (config != null && !Boolean.FALSE.equals(config.get("proxyBeanMethods"))) {
    	beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
    }
    else if (config != null || isConfigurationCandidate(metadata)) {
    	beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
    }
    else {
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这段代码中有一个获取@Configuration注解的proxyBeanMethods属性值的判断,这个其实是Spring配置类的一种类型/模式,具体这里也不分析了有点脱离主题,直接贴一下结论:
    如果 Class上标注有@Component注解、@ComponentScan注解、@Import注解、@ImportResource注解、类上没有任何注解,存在@Bean方法、@Configuration(proxyBeanMethods = false) 这几种情况下认为是lite模式的配置类
    那么对应的full模式就是上面代码中判断的@Configuration(proxyBeanMethods = true)这种情况下认为是 full模式
    那么fulllite模式的区别是什么呢?
    对于full模式:full模式下的配置类会被CGLIB代理生成代理类取代原始类型(在容器中);full模式下的@Bean方法不能是private和final;单例scope下不同@Bean方法可以互相引用,达到单实例的语义
    对于lite模式:lite模式下的配置类不生成代理,原始类型进入容器;lite模式下的@Bean方法可以是private和final;单例scope下不同@Bean方法引用时无法做到单例

    好了关于fulllite模式就简单说这些,感兴趣的小伙伴可以再深入研究,细讲篇幅实在太长了,我们回到正题。

    至此我们分析完了从SpringBoot启动类进入包扫描,然后获取所有扫描到的Bean对象的BeanDefinition,然后递归处理再次解析配置类,当被扫描到的Bean有@Bean注解时,会走org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass方法的@Bean注解处理流程,也就是这部分:

    //这部分代码是org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass的一小段
    //这段专门处理@Bean注解的
    
    // Process individual @Bean methods
    // 通过上面的分析我们知道了,对于我们例子中TestConfig类中有@Bean注解,所以我们这里认为代码运行到parse到TestConfig了
    // 然后下面这一步的sourceClass其实就是TestConfig的SourceClass对象,在这一步获取TestConfig中的@Bean标记的方法
    // 这个就不跟进去分析了  这一步获取到的就是Bean里面所有的@Bean标记的方法
    Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
    // 然后循环把这些添加到configClass记录
    for (MethodMetadata methodMetadata : beanMethods) {
    	configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    那么至此,经过SpringBoot启动类扫描了所有Bean,然后所有扫描出来的Bean也递归的parse完成了,那么就返回到org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions方法中的parser.parse(candidates);这一步,经过这些步骤之后,扫描到的所有的Bean对应的BeanDefinition也注册完成了。然后我们来看接下来该方法中的:this.reader.loadBeanDefinitions(configClasses);

    3.2 this.reader.loadBeanDefinitions(configClasses)解析

    org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions方法中调用this.reader.loadBeanDefinitions(configClasses);时会传入之前已解析到的所有ConfigurationClass,这个Set有很多值,但是我们本次只分析@Bean的内容,所以我们就看TestConfig类的流程。跟进org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader#loadBeanDefinitions方法:

    public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
    	// 传入了所有的ConfigurationClass
    	TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
    	for (ConfigurationClass configClass : configurationModel) {
    		// 由于我们只分析@Bean注解,所以我们就当成本次循环到了TestConfig类,然后跟进下面这个方法
    		loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    跟进loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);方法(假设此时入参是TestConfigConfigurationClass )

    private void loadBeanDefinitionsForConfigurationClass(
    			ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
    
    	if (trackedConditionEvaluator.shouldSkip(configClass)) {
    		String beanName = configClass.getBeanName();
    		if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
    			this.registry.removeBeanDefinition(beanName);
    		}
    		this.importRegistry.removeImportingClass(configClass.getMetadata().getClassName());
    		return;
    	}
    
    	if (configClass.isImported()) {
    		registerBeanDefinitionForImportedConfigurationClass(configClass);
    	}
    	// 前面我们分析的parse方法递归的过程中,TestConfig的@Bean方法会加载成MethodMetadata并添加到了configClass的BeanMethods中
    	// 这里就是把之前解析并记录的BeanMethod 取出来循环load
    	for (BeanMethod beanMethod : configClass.getBeanMethods()) {
    		// load每一个BeanMethod 并转成BeanDefinition注册当工厂中
    		loadBeanDefinitionsForBeanMethod(beanMethod);
    	}
    
    	loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
    	loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    通过上面这段代码我们跟进:loadBeanDefinitionsForBeanMethod(beanMethod);

    private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    	ConfigurationClass configClass = beanMethod.getConfigurationClass();
    	MethodMetadata metadata = beanMethod.getMetadata();
    	String methodName = metadata.getMethodName();
    
    	// Do we need to mark the bean as skipped by its condition?
    	if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
    		configClass.skippedBeanMethods.add(methodName);
    		return;
    	}
    	if (configClass.skippedBeanMethods.contains(methodName)) {
    		return;
    	}
    	
    	//获取@Bean注解的属性值
    	AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
    	Assert.state(bean != null, "No @Bean annotation attributes");
    
    	// Consider name and any aliases
    	// 获取Bean的名称和别名
    	List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
    	// 如果@Bean注解的name属性为空,那么获取@Bean注解标记的方法的名字为beanName  否则就取name属性的第一个的值并将其删除
    	// 在我们的例子TestConfig中,@Bean并没有name属性设置,所以@Bean标记方法生成的spring Bean对象的名称就是方法名:myTest
    	String beanName = (!names.isEmpty() ? names.remove(0) : methodName);
    
    	// Register aliases even when overridden
    	for (String alias : names) {
    		this.registry.registerAlias(beanName, alias);
    	}
    
    	//后续就是一些其他的属性设置和代理相关的  就不一个一个写了
    
    	// Has this effectively been overridden before (e.g. via XML)?
    	if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
    		if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
    			throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
    					beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() +
    					"' clashes with bean name for containing configuration class; please make those names unique!");
    		}
    		return;
    	}
    
    	ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata, beanName);
    	beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    
    	if (metadata.isStatic()) {
    		// static @Bean method
    		if (configClass.getMetadata() instanceof StandardAnnotationMetadata) {
    			beanDef.setBeanClass(((StandardAnnotationMetadata) configClass.getMetadata()).getIntrospectedClass());
    		}
    		else {
    			beanDef.setBeanClassName(configClass.getMetadata().getClassName());
    		}
    		beanDef.setUniqueFactoryMethodName(methodName);
    	}
    	else {
    		// instance @Bean method
    		beanDef.setFactoryBeanName(configClass.getBeanName());
    		beanDef.setUniqueFactoryMethodName(methodName);
    	}
    
    	if (metadata instanceof StandardMethodMetadata) {
    		beanDef.setResolvedFactoryMethod(((StandardMethodMetadata) metadata).getIntrospectedMethod());
    	}
    
    	beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    	beanDef.setAttribute(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.
    			SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
    
    	AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);
    
    	Autowire autowire = bean.getEnum("autowire");
    	if (autowire.isAutowire()) {
    		beanDef.setAutowireMode(autowire.value());
    	}
    
    	boolean autowireCandidate = bean.getBoolean("autowireCandidate");
    	if (!autowireCandidate) {
    		beanDef.setAutowireCandidate(false);
    	}
    
    	String initMethodName = bean.getString("initMethod");
    	if (StringUtils.hasText(initMethodName)) {
    		beanDef.setInitMethodName(initMethodName);
    	}
    
    	String destroyMethodName = bean.getString("destroyMethod");
    	beanDef.setDestroyMethodName(destroyMethodName);
    
    	// Consider scoping
    	ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    	AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class);
    	if (attributes != null) {
    		beanDef.setScope(attributes.getString("value"));
    		proxyMode = attributes.getEnum("proxyMode");
    		if (proxyMode == ScopedProxyMode.DEFAULT) {
    			proxyMode = ScopedProxyMode.NO;
    		}
    	}
    
    	// Replace the original bean definition with the target one, if necessary
    	// 如有必要,将原始 bean 定义替换为代理目标
    	BeanDefinition beanDefToRegister = beanDef;
    	if (proxyMode != ScopedProxyMode.NO) {
    		BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(
    				new BeanDefinitionHolder(beanDef, beanName), this.registry,
    				proxyMode == ScopedProxyMode.TARGET_CLASS);
    		beanDefToRegister = new ConfigurationClassBeanDefinition(
    				(RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata, beanName);
    	}
    
    	if (logger.isTraceEnabled()) {
    		logger.trace(String.format("Registering bean definition for @Bean method %s.%s()",
    				configClass.getMetadata().getClassName(), beanName));
    	}
    	// 到这里 @Bean标记的方法就被加载成BeanDefinition 并注入到工厂中
    	this.registry.registerBeanDefinition(beanName, beanDefToRegister);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118

    那么至此被扫描的配置类中@Bean标记的方法的BeanDefinition也被注册到工厂中了,后续在org.springframework.context.support.AbstractApplicationContext#refresh中实例化的时候,该对象就会被实例化并交由Spring管理,也就分析完了@Bean注解的家在过程。

    最后再说一下本篇分析的是@Configuration注解下@Bean的加载过程,类似@Component注解下也可以加载@Bean,流程和@Configuration基本类似,大家可以debug跟进一下。好了分析完成!

  • 相关阅读:
    第十三届蓝桥杯省赛C/C++ B组
    (还在纠结Notability、Goodnotes和Marginnote吗?)iPad、安卓平板、Windows学习软件推荐
    商鼎云|2022星际文件系统训练营活动回顾
    百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
    Dependabot 开始支持 pub package 版本检测
    独立站源码建站和模板建站如何选择?
    八大排序之交换排序
    linux 单用户模式、^M 坏的解释器
    12、Urban Radiance Fields
    【Python百日进阶-Web开发-Peewee】Day279 - SQLite 扩展(四)
  • 原文地址:https://blog.csdn.net/qq_34203492/article/details/126505543