Spring MVC DispatcherServlet contextConfigLocation设置

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

============================================================= 
DispatcherServlet 的 父类 HttpServletBean 中 init方法,根据servlet生命周期,servlet实例创建后,容器会调用init方法初始化,此方法声明为final 所以在其子类都不能重写,所以当DispatcherServlet 实例创建后,容器调用初始化init方法就是执行的此方法。 
Java代码   收藏代码
  1.  public final void init()  
  2.         throws ServletException  
  3.     {  
  4.         if(logger.isDebugEnabled())  
  5.             logger.debug("Initializing servlet '" + getServletName() + "'");  
  6.         try  
  7.         {  
  8. //ServletConfigPropertyValues是HttpServletBean 的内部类,作用是根据config对象获取servlet中的配置信息,封装为PropertyValue对象放到propertyValueList的list中去  
  9.             org.springframework.beans.PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), requiredProperties);  
  10. //包装成BeanWrapper 对象  
  11.             BeanWrapper bw = new BeanWrapperImpl(this);  
  12.             org.springframework.core.io.ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());  
  13.             bw.registerCustomEditor(org.springframework.core.io.Resource.classnew ResourceEditor(resourceLoader));  
  14.             initBeanWrapper(bw);  
  15. //将封装为PropertyValues 对象的配置信息,设置到bean也就是DispatcherServlet 实例对应的属性中。  
  16.             bw.setPropertyValues(pvs, true);  
  17.         }  
  18.         catch(BeansException ex)  
  19.         {  
  20.             logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);  
  21.             throw ex;  
  22.         }  
  23.         initServletBean();  
  24.         if(logger.isDebugEnabled())  
  25.             logger.debug("Servlet '" + getServletName() + "' configured successfully");  
  26.     }  




Java代码   收藏代码
  1. private static class ServletConfigPropertyValues extends MutablePropertyValues  
  2.     {  
  3.   
  4.         public ServletConfigPropertyValues(ServletConfig config, Set requiredProperties)  
  5.             throws ServletException  
  6.         {  
  7.             Set missingProps = requiredProperties == null || requiredProperties.isEmpty() ? null : ((Set) (new HashSet(requiredProperties)));  
  8. //获取DispatcherServlet 在web.xml中的配置的所有<init-param>的名字  
  9.             Enumeration en = config.getInitParameterNames();  
  10.             do  
  11.             {  
  12.                 if(!en.hasMoreElements())  
  13.                     break;  
  14.                 String property = (String)en.nextElement();  
  15. //获取对应名字的值  
  16.                 Object value = config.getInitParameter(property);  
  17.                 addPropertyValue(new PropertyValue(property, value));  
  18.                 if(missingProps != null)  
  19.                     missingProps.remove(property);  
  20.             } while(true);  
  21.             if(missingProps != null && missingProps.size() > 0)  
  22.                 throw new ServletException("Initialization from ServletConfig for servlet '" + config.getServletName() + "' failed; the following required properties were missing: " + StringUtils.collectionToDelimitedString(missingProps, ", "));  
  23.             else  
  24.                 return;  
  25.         }  
  26.     }  


====================================================================== 
如果忽略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中: 
Java代码   收藏代码
  1. protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {  
  2.     String[] configLocations = getConfigLocations();  
  3.        if (configLocations != null) {  
  4.     for (String configLocation : configLocations) {  
  5.             reader.loadBeanDefinitions(configLocation);  
  6.             }  
  7.         }  
  8.     }  


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

Java代码   收藏代码
  1. protected String[] getConfigLocations() {  
  2. return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());  
  3.     }  


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

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


Java代码   收藏代码
  1. protected String[] getDefaultConfigLocations() {  
  2.    if (getNamespace() != null) {  
  3.  return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};  
  4.         }  
  5.     else {  
  6. return new String[] {DEFAULT_CONFIG_LOCATION};  
  7.         }  
  8.     }  

DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace()+DEFAULT_CONFIG_LOCATION_SUFFIX = 
/WEB-INF/<servlet name>-servlet.xml 
配置Spring MVCDispatcherServlet需要以下步骤: 1. 在web.xml文件中配置DispatcherServlet: ```xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 2. 创建一个名为`applicationContext.xml`的Spring配置文件,并在其中配置Spring MVC相关的组件和属性。例如,可以配置扫描包、视图解析器、处理器映射等: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描包 --> <context:component-scan base-package="com.example.controller" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 处理器映射 --> <mvc:annotation-driven /> </beans> ``` 3. 在`applicationContext.xml`中配置其他需要的组件,例如数据源、事务管理器等。 以上是配置Spring MVCDispatcherServlet的基本步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值