public class Launcher {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
CountDownLatch latch = new CountDownLatch(1);
try {
latch.await();
} catch (InterruptedException ignored) {
}
}
}
这里,只是创建了一个ClassPathXmlApplicationContext的实例,并传入了一个字符串“classpath:spring/spring.xml”,来看它的构造器:
//上面使用的构造器
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}
//最终使用的构造器
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
因为parent为null,所以这里先忽略super(parent);主要做了两件事:setConfigLocations(configLocations)和refresh();
setConfigLocations(configLocations)是在其父类AbstractRefreshableConfigApplicationContext中:
到这里,setConfigLocations(configLocations)就结束了,该方法就是将我们传入的一个表示路径的字符串设置给ClassPathXmlApplicationContext。