相比于BeanFactory接口,我们更多时候使用ApplicationContext接口来加载Bean,两个接口都是来加载Bean的,相比之下ApplicationContext提供了更多扩展功能。使用ApplicationContext方式加载XML
ApplicationContext ctx = new ClassPathXmlApplicationContext("app.xml");
以ClassPathXmlApplicationContext.java为切入点
//其中configLocations为数组形式的xml形式的配置文件路径
public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
this(configLocations, true, null);
}
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
//todo 设置配置文件路径
setConfigLocations(configLocations);
if (refresh) {
//加载刷新配置,可能是xml文件也可能是其他属性文件
refresh();
}
}