Loading both mvc-dispatcher-servlet.xml and applicationContext.xml?

心已入冬 提交于 2020-01-13 23:10:19

问题


I am using Spring 3 MVC. I have both mvc-dispatcher-servlet.xml and applicationContext.xml. But applicationContext.xml is not loading only mvc-dispatcher-servlet.xml is loading.

Any problem with my configuration?

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    version="2.5">  
  <display-name>Archetype Created Web Application</display-name>
  <servlet>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
         </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  

    <servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/extendedContext.xml</param-value>  
    </context-param>  

    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
</web-app>

回答1:


A simpler configuration is to use the import tag in dispatcher servlet XML:

<import resource="classpath:spring/applicationContext.xml" />

It is documented here section 3.2.2.1:

http://docs.spring.io/spring/docs/2.0.8/reference/beans.html

Here is another SO question/answer around this topic:

ContextLoaderListener or not?




回答2:


One option is to Load all the context files using ContextLoader separated by comma.

 <web-app>  
  <display-name>Archetype Created Web Application</display-name>
  <servlet>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>  
   </servlet>
   <servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>*.do</url-pattern>  
   </servlet-mapping>

   <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/mvc-dispatcher-servlet.xml, 
                 classpath:spring/applicationContext.xml,
                 classpath:spring/extendedContext.xml
       </param-value>  
   </context-param>  

   <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
   </listener>
</web-app>


来源:https://stackoverflow.com/questions/22534763/loading-both-mvc-dispatcher-servlet-xml-and-applicationcontext-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!