Deep Dive into Spring Framework

Sadil Chamishka
4 min readOct 10, 2019

The Spring Framework is an application framework and inversion of control container for the Java platform. The below diagram shows the control flow of the request handling and providing response inside the Spring framework.

Front Controller design pattern is used here to handle requests. Dispatcher Servlet is the front controller provides by Spring. Let see how request and response handling is done inside the Spring Framework.

  1. A request arrives at the dispatcher servlet as it was the front controller.

In Spring applications we configure the dispatcher servlet in various ways.

If you are using XML based configurations, in the web.xml file, we add the configurations for the dispatcher servlet.

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>

Or if you are using Annotation based configurations,

public class DispatcherServletInitializer implements   WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {

// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/service/*");
}
}

There are lots of configurations can be made when setting up the dispatcher servlet. but here we have set the front controller to be prefixed with “/service/”. Therefore every request has to be made with the preceding “/service/”.

2. Depend on the request type, corresponding handler mapping is done.

if the request came up to the dispatcher servlet, then it has to forward to the corresponding controller to process it. Dispatcher servlet has that knowledge, as it scans all the controller classes at the server startup.

If you are using XML based configurations, in the dispatcher-servlet.xml file, all the controller classes are scanned.

<context:component-scan base package="com.sadil.webapp"/>

Or if you using Annotation based configurations

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) {
// Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

dispatcherContext.register(SpringWebConfig.class);
}
}
@Configuration
@EnableWebMvc
@ComponentScan("com.sadil.webapp")
public class SpringWebConfig {}

After the server startup, dispatcher servlet has the knowledge to do the handler mapping. Let’s think, there was a controller class inside the package “com.sadil.webapp” and a request comes to dispatcher servlet as “/service/health”, it will be forwarded to the corresponding controller class and request will be processed.

package com.sadil.webapp;@RestController
@RequestMapping("/")
public class HomeController {

@GetMapping("/health")
public String getHealth(){
return "welcome sadil";
}
}

4. If the controller returns a JSP page as the response, we have to get the help of view resolvers to find where is the JSP page is located and etc. If you are developing a rest controller and return the response you don’t need to consider beyond that.

Let’s consider about what is Servlet WebApplication context and Root WebApplication context.

When we create the dispatcher servlet, a servlet context is created for it. It is called as the Servlet WebApplication context. All controllers are includes inside this context. Normally we use only one front controller. But we can create multiple front controllers by creating multiple servlets. “Dispatcher” is just the name we call that servlet. All servlet has it’s own Servlet context including its own controllers.

Root WebApplication context is the context that holds the beans we created in our web application. Servlet WebApplication context can access the Root WebApplication context.

In a simple way, Let’s assume we had only the dispatcher servlet and it’s Servlet WebApplication context, which includes all the controllers. Inside the controller classes, we autowire the service beans (to achieve dependency injection). But service beans reside in Root WebApplication context. As the Servlet WebApplication context can access the Root WebApplication context, the service beans can be autowired without any problem.

Actually, when we create our dispatcher servlet, we configure that this is the Servlet WebApplication context and this is the Root WebApplication context.

Spring provides an easy way of creating a dispatcher servlet with annotation-based configurations as follows.

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringRootConfig.class};
}

protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringWebConfig.class};
}

protected String[] getServletMappings() {
return new String[]{"/service/*"};
}
}
@Configuration @ComponentScan("com.sadil.core")
public class SpringRootConfig{}
@Configuration @ComponentScan("com.sadil.webapp")
public class SpringWebConfig{}

We have set Root WebApplication context, which has stored beans by scanning all classes annotated with @Component, @Service, @Repository,

We have set Servlet WebApplication context, which has stored all the controllers by scanning the classes annotated with @Controller,

Finally, I have set the mapping for the dispatcher servlet by setting “/service/”.

By reading this article, I think you may get an understating of how the Spring framework is working underneath.

--

--