相比於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();
}
}