Esteban S
Esteban S

Reputation: 1919

How do the different contexts in Spring MVC work?

I have the following configuration for my Spring MVC Webapp. I would like to know conceptually what is the difference between the contextConfigLocation in servlet-context.xml (its is the conf for the appServlet) and the others files security, tiles... I don't understand how it works because if I put my tiles-context.xml configuration in servlet-context the app works and in the other case no, but security is working properly. Beans in this files aren't in the appServlet container also? Are there more than one context?

<!-- DispatcherServlet Conf - Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<!-- Spring configuration files in XML -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:security-context.xml
        classpath:tiles-context.xml
        ...
    </param-value>
</context-param>

Upvotes: 2

Views: 244

Answers (1)

Vivek Adhikari
Vivek Adhikari

Reputation: 237

Take a look on

Spring root application context and servlet context confusion

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

for difference about root context and servlet context.

You can define multiple servlet context with configuration like follows

<servlet>
<servlet-name>api-dispatcher</servlet-name>
<servlet-class>
    org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/api-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>

Above context is accessible when url pattern is /rest/*. This is how multiple separate context are configured on spring.

Upvotes: 1

Related Questions