XmlWebApplicationContext实例化过程

在spring中,如何实例化了XmlWebApplicationContext

spring的配置文件加载是以监听的方式加载的xml配置文件

ContextLoader类中有一个静态代码块
static {
   // Load default strategy implementations from properties file.
   // This is currently strictly internal and not meant to be customized
   // by application developers.
   try {
      ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
      defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
   }
   catch (IOException ex) {
      throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
   }
}

DEFAULT_STRATEGIES_PATH="contextLoader.properties";加载的就是这个配置

contextLoader.properties:

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
配置中配置的就是XmlWebApplicationContext

在initWebApplicationContext方法中

if (this.context == null) {
   this.context = createWebApplicationContext(servletContext);
}
protected Class<?> determineContextClass(ServletContext servletContext) {
   String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
   if (contextClassName != null) {
      try {
         return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
      }
      catch (ClassNotFoundException ex) {
         throw new ApplicationContextException(
               "Failed to load custom context class [" + contextClassName + "]", ex);
      }
   }
   else {
      contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
      try {
         return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
      }
      catch (ClassNotFoundException ex) {
         throw new ApplicationContextException(
               "Failed to load default context class [" + contextClassName + "]", ex);
      }
   }
}

contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());这个代码就是从contextLoad.properties配置中加载进来的。XmlWebApplicationContext

 

在springmvc中,如何实例化XmlWebApplicationContext的

springmvc加载配置文件

org.springframework.web.servlet.DispatcherServlet是通过这个servlet,加载配置文件
FrameworkServlet中有一个属性
public static final Class<?> DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;

然而在

protected WebApplicationContext initWebApplicationContext() {

.....

if (wac == null) {
   // No context instance is defined for this servlet -> create a local one
   wac = createWebApplicationContext(rootContext);
}

}

方法中有一个createWebApplicationContext(rootContext);方法

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
   Class<?> contextClass = getContextClass();
   if (this.logger.isDebugEnabled()) {
      this.logger.debug("Servlet with name '" + getServletName() +
            "' will try to create custom WebApplicationContext context of class '" +
            contextClass.getName() + "'" + ", using parent context [" + parent + "]");
   }
   if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
      throw new ApplicationContextException(
            "Fatal initialization error in servlet with name '" + getServletName() +
            "': custom WebApplicationContext class [" + contextClass.getName() +
            "] is not of type ConfigurableWebApplicationContext");
   }
   ConfigurableWebApplicationContext wac =
         (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

   wac.setEnvironment(getEnvironment());
   wac.setParent(parent);
   wac.setConfigLocation(getContextConfigLocation());

   configureAndRefreshWebApplicationContext(wac);

   return wac;
}

Class<?> contextClass = getContextClass();这个方法就是创建XmlWebApplicationContext实例的Class

public Class<?> getContextClass() {
   return this.contextClass;
}

this.contextClass就是前面FrameworkServlet定义的全局变量

 

至此,spring和springmvc创建的上下文实例都是XmlWebApplicationContext

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值