AnnotationConfigApplicationContext
通过查看AnnotationConfigApplicationContext
的源码,发现其有以下几点:
AnnotatedBeanDefinitionReader
this.reader=new AnnotatedBeanDefinitionReader(this);
ClassPathBeanDefinitionScanner
this.scanner=new ClassPathBeanDefinitionScanner(this);
注册组件类register(componentClasses)
或扫描包scan(basePackages)
调用refresh()
方法。
上述流程图如下:
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
通过AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)
向容器内注册了几个创世纪的类:
ConfigurationClassPostProcessor
:用于解析@Configuration
,@ComponentScan
等配置类。@EventListener
注解标记的方法,将其转换为ApplicationListener
。###