springBean注册之@Bean注解解析

前言

本文重点研究@Bean注解的使用与源码解析

从 SpringIOC源码:@Configuration配置类解析过程一文中,Spring通过一系列操作后,会在ConfigurationClassParser类的doProcessConfigurationClass方法中解析@Bean,本文源码讲解就从这里开始

一、@Bean的使用

设置别名,如果未指定,则bean的名称是带注释的方法的名称。如果指定,则忽略方法名,一个bean可以设置多个别名

@Configuration
@EnableWebMvc
public class WebConfig{
    @Bean
    public Son son(){
        return new Son();
    }
}

如果一个类注册了多个bean,我可以使用@Qualifier点名要那个bean

二、@Bean源码解析

1、一阶段解析

class ConfigurationClassParser {

    private final Set<BeanMethod> beanMethods = new LinkedHashSet<>();

	protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)throws IOException {

		//处@Component注解
		
		//处理@PropertySource注解
		
		//处理@ComponentScan注解
		
		// 处理任何@Import注释
	
		// 处理任何@ImportResource注释
		
		// 处理@Bean注解
		//检索所有标注@Bean注解的方法,把他们组装成MethodMetadata
		//注意非配置类中也可以使用@Bean
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
		for (MethodMetadata methodMetadata : beanMethods) {
		    //把他们缓存起来
			configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
		}
	}

2、二阶段解析注册

class ConfigurationClassBeanDefinitionReader {

   public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
		TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
		for (ConfigurationClass configClass : configurationModel) {
			loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
		}
	}
	private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {

		//忽略代码

        //解析@Import
		if (configClass.isImported()) {
			registerBeanDefinitionForImportedConfigurationClass(configClass);
		}
		for (BeanMethod beanMethod : configClass.getBeanMethods()) {
		   //读取给定BeanMethod,注册bean定义使用基于其内容的BeanDefinitionRegistry。
			loadBeanDefinitionsForBeanMethod(beanMethod);
		}

		loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
		loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
	}


  private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
		ConfigurationClass configClass = beanMethod.getConfigurationClass();
		MethodMetadata metadata = beanMethod.getMetadata();
		String methodName = metadata.getMethodName();

		//我们是否需要根据bean的@Conditional条件将其标记为跳过
		if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
			configClass.skippedBeanMethods.add(methodName);
			return;
		}
		if (configClass.skippedBeanMethods.contains(methodName)) {
			return;
		}

		AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
		Assert.state(bean != null, "No @Bean annotation attributes");

		// 考虑名字和任何别名
		List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
		String beanName = (!names.isEmpty() ? names.remove(0) : methodName);

		// 即使重写也要注册别名
		for (String alias : names) {
			this.registry.registerAlias(beanName, alias);
		}

		// 以前是否有效地重写过(例如通过XML)?
		if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
			if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
				throw new BeanDefinitionStoreException("");
			}
			return;
		}

		ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
		beanDef.setResource(configClass.getResource());
		beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
        //如果方法是静态方法
		if (metadata.isStatic()) {
			beanDef.setBeanClassName(configClass.getMetadata().getClassName());
			beanDef.setFactoryMethodName(methodName);
		}
		//如果方法不是静态方法
		else {
			beanDef.setFactoryBeanName(configClass.getBeanName());
			beanDef.setUniqueFactoryMethodName(methodName);
		}
		//设置其自动装配模式为构造注入
		beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
		beanDef.setAttribute(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.
				SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
         //解析@Lazy,@Primary,@DependsOn,@Role,@Description注解
		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);

		// 考虑范围界定
		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;
			}
		}

		// 如有必要,将原始bean定义替换为目标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);
		}

		if (logger.isTraceEnabled()) {
			logger.trace(String.format("");
		}
		//注册到spring中
		this.registry.registerBeanDefinition(beanName, beanDefToRegister);
	}

你可能感兴趣的:(Spring-IOC)