AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Test.class)
类名:AnnotationConfigApplicationContext
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
this();
this.register(annotatedClasses);
this.refresh();
}
this()方法
1、先调用父类GenericApplicationContext的构造函数
public class AnnotationConfigApplicationContext extends GenericApplicationContext
类名:GenericApplicationContext
public GenericApplicationContext() {
this.refreshed = false;
//创建一个容器
this.beanFactory = new DefaultListableBeanFactory();
this.beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
}
2、然后调用自己的构造函数
public AnnotationConfigApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
方法调用链:
this.reader = new AnnotatedBeanDefinitionReader(this);
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment);
AnnotationConfigUtils.registerAnnotationConfigProcessors(BeanDefinitionRegistry registry);
//这个方法插入了internalConfigurationAnnotationProcessor、internalAutowiredAnnotationProcessor、internalRequiredAnnotationProcessor、internalCommonAnnotationProcessor、internalPersistenceAnnotationProcessor这五个Processor
AnnotationConfigUtils.registerAnnotationConfigProcessors(BeanDefinitionRegistry registry, Object source);
this.register(annotatedClasses);
调用链
this.register(annotatedClasses)
AnnotatedBeanDefinitionReader.register(annotatedClasses)
AnnotatedBeanDefinitionReader.registerBean(annotatedClass)
(spring源码02——registerBean源码解析)AnnotatedBeanDefinitionReader.registerBean(annotatedClass, (String)null, (Class[])null);
this.refresh();
spring源码03——AbstractApplicationContext.refresh()