Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of dispatcher servlet xml in Spring

There are many Qs and As on this matter but I have not seen an answer to why my project works. I started it out following a tutorial so it just worked. This is the declaration of my Spring Dispatcher Servlet in web.xml:

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

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

Then I have my servlet definition in a file in the same folder called dispatcher-servlet.xml. But I am not declaring in the web.xml that the definition is to be found in that particular file. How can the web app know that this is the correct file?

I have seen other default names of the file in answers but none like mine. Is dispatcher-servlet.xml a default name that Spring knows about and can find?

like image 885
Jack Flamp Avatar asked Aug 22 '17 12:08

Jack Flamp


1 Answers

From the Spring MVC documentation:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

Since your servlet name is 'dispatcher' Spring looks for the file dispatcher-servlet.xml in the default location of your WEB-INF folder.

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

like image 58
Plog Avatar answered Oct 20 '22 00:10

Plog