【spring之注册bean-AnnotationBeanDefinitionReader】

发布时间:2024年01月05日

1.简介

/**
 * Convenient adapter for programmatic registration of bean classes.
 *
 * <p>This is an alternative to {@link ClassPathBeanDefinitionScanner}, applying
 * the same resolution of annotations but for explicitly registered classes only.
 *
 * /
// 翻译一下就是: 
/**
*	便捷适配器,以编程方式注册bean
*	这是ClassPathBeanDefinitionScanner的一个替换方案,应用吸纳共同的注解解决方案
*	但仅适用于显式注册的类。
*/
public class AnnotatedBeanDefinitionReader {
	// 属性不做解释
	private final BeanDefinitionRegistry registry;

	private BeanNameGenerator beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
	
	private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();

	private ConditionEvaluator conditionEvaluator;
	
	// setter or getter metheods are ignored...
	// constructor metheods are alse ignored...
	
	//	override method ... registerBean
	
	// this is very important metheod named doRegisterBean without value retured
	private <T> void doRegisterBean(Class<T> beanClass,@Nullable String name,
				@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
				@Nullable BeanDefinitionCustomizer[] customizers) {
		// ... see the next section.
	}
}

2. doRegisterBean方法做什么

private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
			@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
			@Nullable BeanDefinitionCustomizer[] customizers) {

		AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
		if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
			return;
		}

		abd.setInstanceSupplier(supplier);
		ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
		abd.setScope(scopeMetadata.getScopeName());
		String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, 
							this.registry));

		AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
		if (qualifiers != null) {
			for (Class<? extends Annotation> qualifier : qualifiers) {
				if (Primary.class == qualifier) {
					abd.setPrimary(true);
				}
				else if (Lazy.class == qualifier) {
					abd.setLazyInit(true);
				}
				else {
					abd.addQualifier(new AutowireCandidateQualifier(qualifier));
				}
			}
		}
		if (customizers != null) {
			for (BeanDefinitionCustomizer customizer : customizers) {
				customizer.customize(abd);
			}
		}

		BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
		definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata,
														 definitionHolder, this.registry);
		BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
	}

3. 详细解析doRegisterBean 方法

/**
*	将传入的class为beanClass,bean名称为name,qualizers 限定符, Bean提供器supplier, 
*	beanDefinition的自定义器应用到容器中去
*/

4. 感悟

Register a bean from the given bean class, deriving its metadata from  class-declared annotations.
文章来源:https://blog.csdn.net/qq_39118371/article/details/135404190
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。