1.通过AnnotationConfigApplicationContext来启动IOC加载
public class SpringIocTest {
public static void main(String[] args) {
ApplicationContext factory = new AnnotationConfigApplicationContext(AnnotationConfigurations.class);
}
}
@Configuration
@ComponentScan("com.zyf.spring")
public class AnnotationConfigurations {
}
2.AnnotationConfigApplicationContext类
类图:
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
//调用构造方法
this();
//将配置类注册成BeanDefinition
register(annotatedClasses);
//核心方法
refresh();
}
AnnotationConfigApplicationContext构造方法
/**
* Create a new AnnotationConfigApplicationContext that needs to be populated
* through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
*/
public AnnotationConfigApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
}