spring bean生命周期和上下文初始化

第一阶段:

1 准备阶段根据BeanDefintionReader去初始化Bean的定义,那么在bean的定义中通常有两种一个是注解Bean定义读取器(AnnotationBeanDefinitionReader),一个是xmlBean定义读取器

BeanDefinition是spring中Bean的元数据定义:

包括:(1)bean的Description;(2)是否懒加载;(3)simpleName;(4)parentName,(5)DependsOn,(6)isSingleton等

过程是: 将上述信息收集到AnnotatedGenericBeanDefinition对象中,然后将该对象和beanName传入BeanDefinitionHolder,最后将BeanDefinitionHolder注册到spring上下文的beanDefinitionMap中:

public void registerBean(Class<?> annotatedClass, String name,
            @SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {

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

        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));
                }
            }
        }

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

 

 


在doCreateBean中的4个方法

1 createBeanInstance
2 populateBean
3 initializaBean
4 DisposableBean

第一步中

posted @ 2020-01-04 12:42  soft.push("zzq")  Views(271)  Comments(0Edit  收藏  举报