spring 系统启动初始化加载

在web.xml的配置

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


系统启动就加载 ContextLoaderListener类的
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}

ContextLoaderListener类也继承ContextLoader类,所以就把自己设置给contextLoader变量。

进入ContextLoader类的初始化

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);


ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}


if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}

return this.context;

}


上面方法,先实例化WebApplicationContext的子类,然后调用另一方法(看意思就是先配置该类和唤醒该类)

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
if (idParam != null) {
wac.setId(idParam);
}
else {
// Generate default id...
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet <= 2.4: resort to name specified in web.xml, if any.
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getServletContextName()));
}
else {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
}


// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(sc);


wac.setParent(parent);
wac.setServletContext(sc);
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
wac.setConfigLocation(initParameter);
}
customizeContext(sc, wac);
wac.refresh();
}

上面就调用的WebApplicationContext类的refresh方法

// 准备这个背景刷新。
prepareRefresh();


// 告诉子类刷新内部bean工厂。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();


// 准备在这方面的使用bean工厂。
prepareBeanFactory(beanFactory);


try {
// 允许在上下文中的子类的bean工厂后处理。
postProcessBeanFactory(beanFactory);


// 调用工厂处理器登记为上下bean。
invokeBeanFactoryPostProcessors(beanFactory);


// 注册bean处理器拦截bean的创建。
registerBeanPostProcessors(beanFactory);


// 初始化此上下文信息源。
initMessageSource();


// 初始化事件多路广播此上下文。
initApplicationEventMulticaster();


// 初始化其他特殊豆在具体语境中的子类。
onRefresh();


// 检查监听器豆和注册。
registerListeners();


// 实例化所有剩余的(非懒惰初始化)单例。
finishBeanFactoryInitialization(beanFactory);


// 最后一步:发布相应的事件。
finishRefresh();
}


catch (BeansException ex) {
//Destroy already created singletons to avoid dangling resources.
destroyBeans();


// Reset 'active' flag.
cancelRefresh(ex);


// Propagate exception to caller.
throw ex;
}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值