Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of loading contextConfigLocation in web.xml of Spring Servlet project

Tags:

Suppose that I have a Spring Java project and I am trying to configure it as a web server servlet. Here is a stripped-down version of the web.xml file:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

The key thing to note here is that I have specified two XML files to be loaded. One is general for my entire application, while the other is specific to the "my-servlet" servlet. For a setup with just one servlet-mapping, this wouldn't make sense. However, my project has multiple servlet-mappings and each one has specific Spring settings to them.

My Question: Which contextConfigLocation is going to be loaded first by Spring? Will it be the generalApplicationContext.xml or will it be the specificApplicationContext.xml? More importantly, does the order of loading even matter? From my debugging efforts, it seems apparent that it does because I get different errors when I move some independent Spring configuration from one file to the other.

NB: Whether or not using multiple spring configurations for multiple servlet mappings is a good practice is debatable. Same goes for using XML config instead of the new Java config. But that's not what I'm trying to ask here. Let's try to focus on my main question.

like image 267
ecbrodie Avatar asked Dec 18 '14 05:12

ecbrodie


People also ask

Which context is loaded first?

Here is how the logs would typically look like - clearly the root context is loaded first and is set as the parent for the context heirarchy - the servlet context is loaded next.

What is contextConfigLocation in web xml?

The initialization parameter contextConfigLocation tells Spring where to load configuration files. The <load-on-startup> tag tells the servlet container to load this servlet upon start up with highest priority.

What is the use of Org Springframework web context ContextLoaderListener in web xml?

As per my understanding, ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web. xml), parses it and loads the singleton bean defined in that config file. Similarly when we want to load prototype bean, we will use same webapplication context to load it.

What is the difference between DispatcherServlet and ContextLoaderListener in Spring?

Whereas DispatcherServlet is expected to load beans containing web components such as controllers, view resolvers, and handler mappings, ContextLoaderListener is expected to load the other beans in your application.


2 Answers

generalApplicationContext.xml is the one that will be loaded first because it is the ApplicationContext loaded with the ContextLoaderListener

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>

specificApplicationContext.xml is actually a Child Context of the Above loaded generalApplicationContext.xml and it will be a WebApplicationContext

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

And yes the order of loading does matter. Because when the parent context is loaded all the required dependencies must be fulfilled.

like image 51
shazin Avatar answered Dec 30 '22 22:12

shazin


The below part loads the context file and create the ApplicationContext. This context might, for instance, contain components such as middle-tier transactional services, data access objects, or other objects that you might want to use (and re-use) across the application. There will be one application context per application.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>

The other context is the WebApplicationContext which is the child context of the application context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. The initialization of the WebApplicationContext happens like this:

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

For more details refer this and this

like image 31
Ankur Singhal Avatar answered Dec 30 '22 21:12

Ankur Singhal