2

What is the purpose of each function. Why spring has given two different functions for configuration classes? I am confused between two, which one should I use?

Akshay Bande
  • 2,491
  • 2
  • 12
  • 29

2 Answers2

5

In a typical Spring application there are 2 ApplicationContext instances, one is the so called root application context and the second (or third or ...) is the servlet application context.

The root application typically contains shared/general resources like DataSource, services, repositories, etc... The servlet context contains beans specific for this context, generally things like view resolver, handler mappings, controllers etc. The servlet context uses the root context as a parent and thus can see the beans defined in there (the root doesn't know about the servlet contexts!).

In this typical setup the root context is loaded by the ContextLoaderListener and the servlet context by the DispatcherServlet.

In the old days one would write a web.xml which would contain a servlet-listener for the ContextLoaderListener and a servlet element for the DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
>

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

    <!-- Spring child -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

The ContextLoaderListener will load the applicationContext.xml by default and the DispatcherServlet a <servlet-name>-servlet.xml (hence a dispatcher-servlet.xml).

As of the Servlet 3.0 spec it is possible to replace the web.xml with a Java based configuration. Spring has taken the time to provide base classes which already do the basic configuration (like the registration of the ContextLoaderListener and DispatcherServlet). However as it is now fully Java based configuration both the ContextLoaderListener and DispatcherServlet need to be provided with a list of configuration classes, as there is no default class name it can detect to load.

So the getRootConfigClasses() will configure the ContextLoaderListener and is actually optional (you can return null or an empty array). The getServletConfigClasses() will configure the DispatcherServlet (and is required).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.html

createRootApplicationContext() The returned context ... will be established as the parent context for any DispatcherServlet application contexts. As such, it typically contains middle-tier services, data sources, etc.

createServletApplicationContext() The returned context ... typically contains controllers, view resolvers, locale resolvers, and other web-related beans.

Comprehensive difference explanation between servlet and root context: What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

Michal W
  • 318
  • 4
  • 11
  • The last link doesn't explain the different between a servlet and root context. In this case both the root and servlet context are both `WebApplicationContext` instances. – M. Deinum Jun 17 '19 at 06:43