0

Why there must be <init-param> in <servlet> node with the <param-name> "contextConfigLocation" ? Because if comment all the <init-param> group then a project will not load?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <!-- Spring MVC dispatcher servlet -->
    <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></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Loads Spring configurations from files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-mvc.xml,
            /WEB-INF/spring-security.xml,
            /WEB-INF/hibernate-context.xml
        </param-value>
    </context-param>

        <!-- Spring Security filter -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
thinker
  • 402
  • 1
  • 6
  • 15

1 Answers1

0

What is the right place for spring configuration files ?

You can load the spring configuration files either through <context-param> or <init-param> of the servlet class. But, there is a difference as explained below:

If you are loading using <context-param>, then the dependencies (spring beans) will be available for the whole application (for all servlets). This will be used to load the common dependencies across the whole application (like Services, DAOs, etc..).

But, if you are loading using <init-param> of DispatcherServlet, then the dependencies (spring beans) will be available only to the DispatcherServlet as a child context. So, if you have multiple entry end points for your application (like Restlet or Apache CXFServlet etc..), you can maintain child contexts one per each servlet (entry point). Each child context owns/loads the specific dependencies for the respective web tier.

You can look at the below notes taken from Spring's WebApplicationContext API:

Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

You can look here and here

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67