2

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>
user755806
  • 6,565
  • 27
  • 106
  • 153

2 Answers2

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?

Community
  • 1
  • 1
Kevin
  • 24,871
  • 19
  • 102
  • 158
  • Note that this is a completely different configuration than using a ContextLoaderListener. – Sotirios Delimanolis Mar 20 '14 at 13:55
  • Kevin, then shall i remoe ContextLoaderListener ? – user755806 Mar 20 '14 at 13:59
  • Yes, you remove it and import in the dispatcher servlet xml instead. – Kevin Mar 20 '14 at 14:00
  • You don't need a ContextLoaderListener if your configuration file name is ApplicationContext and the file location is the default location. else you need to use the ContextLoaderListener and provide the exact location – shazinltc Mar 20 '14 at 14:01
  • I have overridden ContextLoaderListener. If i dont use then i loose some functionlality.. – user755806 Mar 20 '14 at 14:06
  • your `` in web.xml is not the normal one then? – Kevin Mar 20 '14 at 14:10
  • I have overridden ContextLoaderListener to resolve some property file names. Ex: i have a property file names as appprop-{env}.props. In Web.xml i give env dev/uat/prod . overridden contextloader listener would replace {env} with value. Now how can i resolve it if i dont use context loader lister? – user755806 Mar 20 '14 at 14:21
  • have you ever used ``? I have typically managed this outside of spring: you put a standard name as the location for the `property-placeholder`, such as my-app.properties, and then you create a symbolic link from your environment specific name to the standard name. This allows you to switch out properties by updating a symlink rather than a code launch. – Kevin Mar 20 '14 at 14:34
  • If you don't like the symlink approach, you can use environment variables in your location as well, see: http://stackoverflow.com/questions/1841857/can-i-use-an-environment-variable-based-location-for-spring-filesystemresource. – Kevin Mar 20 '14 at 14:38
1

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>
devikiran
  • 36
  • 4