48

Ok, I am 500th user asking this question, I read many answers but still having no luck.

parent module pom contains:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.framework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.framework.version}</version>
</dependency>

Child module has maven-jetty-plugin and I run my webapp module with jetty:run.

web.xml defines standard dispatcher module:

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

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I have file dispatcher-servlet.xml under WEB-INF, though start fails with:

FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

What is wrong? Documentation and everybody says that Spring MVC will search for XX-servlet.xml, where XX is name of servlet. Why does it search for applicationContext.xml?

Community
  • 1
  • 1
Leos Literak
  • 8,805
  • 19
  • 81
  • 156

2 Answers2

72

ContextLoaderListener has its own context which is shared by all servlets and filters. By default it will search /WEB-INF/applicationContext.xml

You can customize this by using

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/somewhere-else/root-context.xml</param-value>
</context-param>

on web.xml, or remove this listener if you don't need one.

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • It is not shared but becomes the parent context or root context. Servlet contexts become children and thus can see beans in the parent but not the other way around. Which might have been what you meant. – Adam Gent Mar 12 '14 at 12:13
  • This solved a completely different issue that has been plaguing me for the last day. Good on you. – DasBooten Mar 03 '16 at 17:33
  • What is "somewhere-else" supposed to be - sorry, im completely new to this? – Vic Torious Jan 05 '17 at 01:48
25

Update: This will create a second context same as in applicationContext.xml

or you can add this code snippet to your web.xml

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

instead of

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
Aalkhodiry
  • 2,178
  • 30
  • 25