java web启动加载_web应用启动的时候SpringMVC容器加载过程

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-config.xml

1

springmvc

/

上面是在web.xml配置文件中加载springmvc配置文件生成webapplicationcontext容器的经典配置。

我们可以看到,在web应用一启动,该DispatcherServlet就被加载了,加载的时候提供了contextConfigLoacation的初始值,然后通过类的全限定类名使用反射加载该类。

/*** Set the context config location explicitly, instead of relying on the default

* location built from the namespace. This location string can consist of

* multiple locations separated by any number of commas and spaces.*/

public voidsetContextConfigLocation(String contextConfigLocation) {this.contextConfigLocation =contextConfigLocation;

}

以上代码是在DispatcherSevlet父类FrameworkServlet中,可以看到提供contextConfigloaction后该类就有了srpingmvc配置文件路径。

protectedWebApplicationContext 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 newApplicationContextException("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);returnwac;

}

以上代码是DispatcherSevlet父类FrameworkServlet中的创造webapplicationcontext的方法,在方法中可以看到获得刚刚设置的springmvc配置文件的路径,并且set到ConfigurableWebApplicationContext用于创建容器。

大体流程就是这样,现在做下小总结:

当web应用启动的时候配置在web.xml中的DisPartcherServlet就被加载,怎么被加载的呢?

通过提供该类的全限定类名和初始化参数使用反射加载。加载后生成了webApplicationContext容器,

通过该容器就可以使用ioc、aop等spring功能了。需要说明的是该容器的生成需要sevlectContext,

因此必须得再拥有web容器的前提下才能加载webApplicationContext容器。因此,即使它继承自

applicationContext,因此它的初始化方式也与applicationContext和BeanFactory不同

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值