【spring之注册bean-AnnotationBeanDefinitionReader】

Spring之注册Bean-AnnotationBeanDefinitionReader

  • 1.简介
  • 2. doRegisterBean方法做什么
  • 3. 详细解析doRegisterBean 方法
  • 4. 感悟

1.简介

/**
 * Convenient adapter for programmatic registration of bean classes.
 *
 * 

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.

你可能感兴趣的:(spring全家桶,spring)