spring mvc 配置失效了?

版本:spring 3.0以上html

项目中有两个spring的配置xml,以下java

${project.dir}\src\main\resources\META-INF\spring\applicationContext-service-database.xml
${project.dir}\src\main\webapp\WEB-INF\database-servlet.xml

项目中的web.xmlweb

${project.dir}\src\main\webapp\WEB-INF\web.xml

web.xml的配置以下:spring

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:META-INF/spring/applicationContext-service-database.xml
    </param-value>
</context-param> 
<listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
 
<servlet>
    <servlet-name>database</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>database</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

一、ContextLoaderListener加载applicationContext-service-database.xml

二、DispatcherServlet加载database-servlet.xml。若是不配置contextConfigLocation的话,DispatcherServlet会默认查找${project.dir}\src\main\webapp\WEB-INF\{servlet-name}-servlet.xml。

      如本例:servlet-name=database,则文件名为database-servlet.xml,全部会查找${project.dir}\src\main\webapp\WEB-INF\database-servlet.xml

具体查看文档https://docs.spring.io/spring/docs/3.2.18.RELEASE/spring-framework-reference/htmlsingle/#mvc-introduction中17.2api

三、这样配置最终spring会产生两个容器,ContextLoaderListener会生成容器A,DispatcherServlet会生成容器B,容器B的父容器是容器A,容器A的父容器为null

四、容器B会继承容器A的部分功能(其实这样作主要是为了防止单例bean的重复注入和容器B能够获取到容器A的bean,这样进行bean共享,可是容器A获取不到容器B的bean)

五、这样的配置spring mvc就有了,不须要在database-servlet.xml中配置<mvc:annotation-driven/>

问题:

若是在applicationContext-service-database.xml配置<mvc:annotation-driven/>,而在database-servlet.xml没有配置<mvc:annotation-driven/>,这样就会致使spring mvc功能失效,访问地址会404

缘由:

由于若是在applicationContext-service-database.xml配置<mvc:annotation-driven/>,就会致使ContextLoaderListener加入容器A提早把spring mvc加载到容器A中,可是此时容器A,并无database-servlet.xml中对应须要向外暴露的接口、url或controller。当DispatcherServlet加载容器B的时候发现,它的父容器A中已经存在spring mvc的相关功能,就不会重复加载,因此就会致使spring mvc的功能失效

关键代码:mvc

org.springframework.web.servlet.DispatcherServlet
private void initHandlerMappings(ApplicationContext context) {
   this.handlerMappings = null;
 
   if (this.detectAllHandlerMappings) {
      // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
      // BeanFactoryUtils.beansOfTypeIncludingAncestors 会去获取容器B和它的父容器A中的HandlerMapping的接口实现,若是ContextLoaderListener已经配置了<mvc:annotation-driven/>
      // ,则直接使用ContextLoaderListener加载的HandlerMapping
      Map<String, HandlerMapping> matchingBeans =
            BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
      if (!matchingBeans.isEmpty()) {
         this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
         // We keep HandlerMappings in sorted order.
         OrderComparator.sort(this.handlerMappings);
      }
   }
   else {
      try {
         HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
         this.handlerMappings = Collections.singletonList(hm);
      }
      catch (NoSuchBeanDefinitionException ex) {
         // Ignore, we'll add a default HandlerMapping later.
      }
   }
 
   // Ensure we have at least one HandlerMapping, by registering
   // a default HandlerMapping if no other mappings are found.
   // 若是在任何配置文件中都没有配置spring mvc 功能,如:<mvc:annotation-driven/>,则会使用默认的HandlerMapping,默认的HandlerMapping是
   //org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping, org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
   // 相关默认配置请查看org\springframework\web\servlet\DispatcherServlet.properties
   if (this.handlerMappings == null) {
      this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
      if (logger.isDebugEnabled()) {
         logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
      }
   }
}

这只是spring mvc 其中一个功能,其余相关功能请查看DispatcherServletinit*方法app

解决办法:
不要在ContextLoaderListener加载任何与spring mvc相关的功能

最后
ContextLoaderListener就是为了加载公用的类而存在的
通常ContextLoaderListener只有一个,而DispatcherServlet能够有多个,每一个DispatcherServlet都会默认的以ContextLoaderListener加载的容器为父容器。

因此请按照spring 的套路来,减小没必要须的问题

相关文章
相关标签/搜索