加载mvc-dispatcher-servlet.xml和applicationContext.xml?

时间:2014-03-20 13:40:04

标签: java spring spring-mvc spring-3

我正在使用Spring 3 MVC。 我有mvc-dispatcher-servlet.xml和applicationContext.xml。 但是applicationContext.xml只是加载mvc-dispatcher-servlet.xml才加载。

我的配置有问题吗?

的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>

2 个答案:

答案 0 :(得分:1)

更简单的配置是在调度程序servlet XML中使用import标记:

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

这里记录了3.2.2.1节:

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

这是关于这个主题的另一个问题/答案:

ContextLoaderListener or not?

答案 1 :(得分:1)

一个选项是使用以逗号分隔的ContextLoader加载所有上下文文件

 <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>