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方法就是执行的此方法。
======================================================================
如果忽略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中:
getConfigLocations()获取配置文件的位置,实际调用了父类AbstractRefreshableConfigApplicationContext
中的:
如果configLocations 已经有值就返回,没有调用getDefaultConfigLocations()方法获取默认的位置;
而getDefaultConfigLocations()方法由子类XmlWebApplicationContext重写:
DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace()+DEFAULT_CONFIG_LOCATION_SUFFIX =
/WEB-INF/<servlet name>-servlet.xml
其中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方法就是执行的此方法。
- 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