DEV Community

PGD
PGD

Posted on

Specifying context configuration location for DispatcherServlet

The DispatcherServlet is a front controller serving as a single point entry of request. In Spring MVC architecture, every request passes through DispatcherServlet. Once the DispatcherServlet receives the request then it recognizes the path, gets a controller mapped to the path, and lets the controller process the business logic.

Since DispatcherServlet is a front controller, which is a single point entry, it's the only servlet to register in web application context. We can register it by specifying inside of web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
Enter fullscreen mode Exit fullscreen mode

Also it's possible to tell DispatcherServlet where the Spring config file is by specifying an whose param name is "contextConfigLocation". If I have a Spring config file named "context-common.xml" on the root directory of web application, then I can specify the location as follows.

<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/context-common.xml</param-value>
</init-param
Enter fullscreen mode Exit fullscreen mode

Also I can put the config file at the resources file and prepend "classpath:" at the config file path so that DispatcherServlet knows the config file places on the classpath.

DispatcherServlet lookups spring beans of controllers using WebApplicationContext. By default, its implementation the DispatcherServlet is using is org.springframework.web.context.support.XmlWebApplcationContext. That receives an XML configuration file for initial config file.
If I want to use a Java class annotated with @Configuration to configure Spring context, I have to say to DispatcherServlet that it should make use of other implementation of WebApplicationContext. AnnotationWebConfigApplicationContext is needed in order for the Spring Container to contain the configuration bean. It is possible to configure that in web.xml:

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.springmvc.demo.web.config.WebMVCConfig</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →