好记性不如烂笔头。记一下对ApplicationContext实例化的过程的吧。
代码:
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
首先进入的代码是:
static {
ContextClosedEvent.class.getName();
}
这个代码块在AbstractApplicationContext里,是ClassPathXmlApplicationContext的父类,所以静态代码块先运行。(所以这句代码的作用是啥呢?求大佬指导)
进入代码:
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[]{configLocation}, true, (ApplicationContext)null);
}
之后调用本类的另一个构造方法
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
super(parent);
this.setConfigLocations(configLocations);
if (refresh) {
this.refresh();

本文详细记录了ApplicationContext的实例化步骤,从静态代码块开始,经过ClassPathXmlApplicationContext的构造方法,逐级调用父类构造方法,直至refresh方法,其中refresh()方法是Spring容器初始化的核心。
最低0.47元/天 解锁文章
3281

被折叠的 条评论
为什么被折叠?



