Spring MVC DispatcherServlet contextConfigLocation设置

[/code]
web.xml Spring 控制器配置
其中contextConfigLocation是怎么 读取 并设置到 DispatcherServlet 属性中
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>[color=red]contextConfigLocation[/color]</param-name>
<param-value>/WEB-INF/Config.xml</param-value>
</init-param>
</servlet>

=============================================================
DispatcherServlet 的 父类 HttpServletBean 中 init方法,根据servlet生命周期,servlet实例创建后,容器会调用init方法初始化,此方法声明为final 所以在其子类都不能重写,所以当DispatcherServlet 实例创建后,容器调用初始化init方法就是执行的此方法。
[code="java"]
public final void init()
throws ServletException
{
if(logger.isDebugEnabled())
logger.debug("Initializing servlet '" + getServletName() + "'");
try
{
//ServletConfigPropertyValues是HttpServletBean 的内部类,作用是根据config对象获取servlet中的配置信息,封装为PropertyValue对象放到propertyValueList的list中去
org.springframework.beans.PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), requiredProperties);
//包装成BeanWrapper 对象
BeanWrapper bw = new BeanWrapperImpl(this);
org.springframework.core.io.ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(org.springframework.core.io.Resource.class, new ResourceEditor(resourceLoader));
initBeanWrapper(bw);
//将封装为PropertyValues 对象的配置信息,设置到bean也就是DispatcherServlet 实例对应的属性中。
bw.setPropertyValues(pvs, true);
}
catch(BeansException ex)
{
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
throw ex;
}
initServletBean();
if(logger.isDebugEnabled())
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}




private static class ServletConfigPropertyValues extends MutablePropertyValues
{

public ServletConfigPropertyValues(ServletConfig config, Set requiredProperties)
throws ServletException
{
Set missingProps = requiredProperties == null || requiredProperties.isEmpty() ? null : ((Set) (new HashSet(requiredProperties)));
//获取DispatcherServlet 在web.xml中的配置的所有<init-param>的名字
Enumeration en = config.getInitParameterNames();
do
{
if(!en.hasMoreElements())
break;
String property = (String)en.nextElement();
//获取对应名字的值
Object value = config.getInitParameter(property);
addPropertyValue(new PropertyValue(property, value));
if(missingProps != null)
missingProps.remove(property);
} while(true);
if(missingProps != null && missingProps.size() > 0)
throw new ServletException("Initialization from ServletConfig for servlet '" + config.getServletName() + "' failed; the following required properties were missing: " + StringUtils.collectionToDelimitedString(missingProps, ", "));
else
return;
}
}



======================================================================
如果忽略contextConfigLocation此设定,则默认为“/WEB-INF/<servlet name>-servlet.xml”,其中<servlet name>以Servlet 名替换

HttpServletBean 中 init方法中:
initServletBean()方法有子类FrameworkServlet重写
在FrameworkServlet的initServletBean()方法中,
this.webApplicationContext = initWebApplicationContext();
完成DispatcherServlet 使用的webApplicationContext 的创建和初始化。
具体创建由FrameworkServlet的:
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) 方法实现,
在方法最后调用wac.refresh();初始化完成资源文件定位,解析配置文件生成Document对象,从这个对象中解析各个bean定义生成beanDefinition对象,最后注册到该WebApplicationContext 容器中

在AbstractApplicationContext中提供了refresh()方法的实现。。。。。

在XmlWebApplicationContext中:

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}


getConfigLocations()获取配置文件的位置,实际调用了父类AbstractRefreshableConfigApplicationContext
中的:


protected String[] getConfigLocations() {
return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}


如果configLocations 已经有值就返回,没有调用getDefaultConfigLocations()方法获取默认的位置;

而getDefaultConfigLocations()方法由子类XmlWebApplicationContext重写:



protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
}
else {
return new String[] {DEFAULT_CONFIG_LOCATION};
}
}

DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace()+DEFAULT_CONFIG_LOCATION_SUFFIX =
/WEB-INF/<servlet name>-servlet.xml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值