关于ContextLoaderListener源码

 

ContextLoaderListener继承ServletContextListener类,实现contextInitialized方法

public void contextInitialized(ServletContextEvent event) {
  this.contextLoader = createContextLoader();
  this.contextLoader.initWebApplicationContext(event.getServletContext());
 }

创建contextLoader,调用initWebApplicationContext方法:

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

   // Store context in local instance variable, to guarantee that
   // it is available on ServletContext shutdown.
   this.context = createWebApplicationContext(servletContext, parent);

//createWebApplicationContext方法还回默认的XmlWebApplicationContext
   servletContext.setAttribute(
     WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
//并放入ServletContext中

跟踪到createWebApplicationContext(),

Class contextClass = determineContextClass(servletContext);
  //Return the WebApplicationContext implementation class to use,
  //either the default XmlWebApplicationContext or a custom context class if specified
  if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
   throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
     "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
  }

  ConfigurableWebApplicationContext wac =
    (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
  //  BeanUtils.instantiateClass is Convenience method to instantiate a class using its no-arg constructor
  wac.setParent(parent);
  wac.setServletContext(servletContext);
  String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
  //configLocation = /WEB-INF/applicationContext.xml;
  if (configLocation != null) {
   wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
     ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
   //Set the config locations for this web application context.
   //If not set, the implementation is supposed to use a default
   //for the given namespace or the root web application context, as appropriate.
  }

  wac.refresh();
  return wac;
 最后返回XmlWebApplicationContext实例;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值