Xml格式的应用启动
Xml和注解是我们进行使用Spring进行开发时用到的两种主要配置方式。本文简单介绍Xml格式的应用的启动流程。
从启动应用的代码开始入手:
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中:
其实this.configLocations最终就是我们传入的那个字符串,即:
到这里,setConfigLocations(configLocations)就结束了,该方法就是将我们传入的一个表示路径的字符串设置给ClassPathXmlApplicationContext。
再来看refesh(),该方法是接口ConfigurableApplicationContext中定义的方法,是应用真正启动的方法,所以这个方法非常重要。
具体到ClassPathXmlApplicationContext,这个refesh()的实现是在其父类AbstractApplicationContext中: