spring笔记-AnnotatedBeanDefinitionReader

分析这个类,主要是为了分析AnnotationConfigApplicationContext注解加载解析流程

1.示例代码

当我们执行以下代码时,MyBeanConfig3为主要配置的入口点

@Configuration
public class MyBeanConfig3 {

    @Bean()
    public Country country1(){
        return new Country();
    }

    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country1());
    }
}

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(
                MyBeanConfig3.class);
        MyBeanConfig3 config3=context.getBean(MyBeanConfig3.class);
    }
}

2.注册执行流程

以上代码会走到AnnotationConfigApplicationContext构造函数中调用AnnotatedBeanDefinitionReader的register,所以AnnotatedBeanDefinitionReader是重点关注的流程

    public AnnotationConfigApplicationContext() {
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

    public AnnotationConfigApplicationContext(Class... annotatedClasses) {
        this();
        register(annotatedClasses);
        refresh();
    }

    public void register(Class... annotatedClasses) {
        Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
        this.reader.register(annotatedClasses);
    }

3.AnnotatedBeanDefinitionReader

其主要做了2件事情

1.注册内置BeanPostProcessor
2.注册相关的BeanDefinition

3.1 注册内置BeanPostProcessor

这个为后续解析Bean和Configuration注解做准备

    public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        Assert.notNull(environment, "Environment must not be null");
        this.registry = registry;
        this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
    }

3.2.注册相关的BeanDefinition

这个是我们关注的重点,其实是生成并注册一个AnnotatedGenericBeanDefinition对象

     void doRegisterBean(Class annotatedClass, @Nullable Supplier instanceSupplier, @Nullable String name,
            @Nullable Class[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {

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

        abd.setInstanceSupplier(instanceSupplier);
        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 qualifier : qualifiers) {
                if (Primary.class == qualifier) {
                    abd.setPrimary(true);
                }
                else if (Lazy.class == qualifier) {
                    abd.setLazyInit(true);
                }
                else {
                    abd.addQualifier(new AutowireCandidateQualifier(qualifier));
                }
            }
        }
        for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
            customizer.customize(abd);
        }

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

参考:
https://www.jianshu.com/p/86b53e3f0264

你可能感兴趣的:(spring笔记-AnnotatedBeanDefinitionReader)