Understanding spring dispatcher servlet initialization

This is how the spring documentation recommends initializing the dispatcherServlet:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>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>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

      

My question is about providing an empty param value in the tag init-param

. Even though this parameter is equal context-param

, we still provide an empty value. So there contextConfigLocation

should be null

when going to the servlet method init()

. What's wrong, please correct me.

+3


source to share


1 answer


Spring web applications have two types of container, each configured and initialized differently.

  • Application context
  • Web application context

The application context is initialized with a config file that you specified as context parameters and displayed the ContextLoaderListener. This is purely what I would consider to be the business logic associated with beans.



A web application context is a child application context that may or may not be present. Each DispatcherServlet will have an associated WebApplicationContext and that takes Spring beans from your initialization parameters to create the context.

Regardless of what beans are available in the ApplicationContext, each WebApplicationContext can be referenced.

The reason we have two different bean configurations is to provide a clear separation between middle tier services such as business logic components and data access classes (which are usually defined in ApplicationContext) and web component related components such as controllers and view resolvers (which are defined in the WebApplicationContext for the Servlet dispatcher).

+8


source







All Articles