AnnotationConfigApplicationContext

What

AnnotationConfigApplicationContext

How

通过查看AnnotationConfigApplicationContext的源码,发现其有以下几点:

this.reader=new AnnotatedBeanDefinitionReader(this);
this.scanner=new ClassPathBeanDefinitionScanner(this);

上述流程图如下:

flowchart TD
    subgraph one["AnnotationConfigApplicationContext"]
    a1["this.reader = new AnnotatedBeanDefinitionReader(this)"]-->a2["this.scanner = new ClassPathBeanDefinitionScanner(this)"]
    a2-->a31["register(componentClasses)"]
    a2-->a32["scan(basePackages)"]
    a31-->a4["refresh()"]
    a32-->a4
    end

核心源码如下:

package org.springframework.context.annotation;

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {

    private final AnnotatedBeanDefinitionReader reader;

    private final ClassPathBeanDefinitionScanner scanner;

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

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

    public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
        this();
        register(componentClasses);
        refresh();
    }

    public AnnotationConfigApplicationContext(String... basePackages) {
        this();
        scan(basePackages);
        refresh();
    }

}

AnnotatedBeanDefinitionReader

AnnotatedBeanDefinitionReader通过AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)向容器内注册了几个创世纪的类:

###