Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
198 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...