Three Tier (Three Layer) Architecture in Spring MVC Web Application

In this article, we will discuss how to create three-layer architecture in Spring MVC web applications.

In this article, we will discuss:

  1. Three Tier (Three Layer) Architecture
  2. Three Tier (Three Layer) Architecture VS MVC Pattern
  3. How to use Three-layer architecture in Spring MVC web applications.

1.Three Tier (Three Layer) Architecture

Three-tier (or three-layer) architecture is a widely accepted solution to organize the codebase. According to this architecture, the codebase is divided into three separate layers with distinctive responsibilities:

Presentation layer: This is the user interface of the application that presents the application’s features and data to the user.

Business logic (or Application) layer:This layer contains the business logic that drives the application’s core functionalities. Like making decisions, calculations, evaluations, and processing the data passing between the other two layers.

Data access layer (or Data) layer:This layer is responsible for interacting with databases to save and restore application data.

2.Three Tier (Three Layer) Architecture VS MVC Pattern

Let's see how these two architectural patterns (both containing three connected components) relate to each other.

The MVC pattern is only concerned with organizing the logic in the user interface (presentation layer). As the name implies, the MVC pattern has three layers: The Model defines the business layer of the application, the Controller manages the flow of the application, and the View defines the presentation layer of the application.

  1. The Model Layer - This is the data layer which contains the business logic of the system, and also represents the state of the application. It’s independent of the presentation layer, the controller fetches the data from the Model layer and sends it to the View layer.
  2. The Controller Layer -The controller layer acts as an interface between View and Model. It receives requests from the View layer and processes them, including the necessary validations.
  3. The View Layer -This layer represents the output of the application, usually some form of UI. The presentation layer is used to display the Model data fetched by the Controller.

Three-tier architecture has a broader concern. It’s about organizing the code in the whole application.

The controller component of MVC is the connection point between the two layers:

3. How to use Three-layer architecture in Spring MVC web applications.

In a Spring MVC web application, the three layers of the architecture will manifest as follows:

Consider below Spring MVC web application using Spring boot and thymeleaf. We have created a three-layer architecture and each layer is mapped to the corresponding package. For example:

Conclusion

In this article, we have discussed:

  1. Three Tier (Three Layer) Architecture
  2. Three Tier (Three Layer) Architecture VS MVC Pattern
  3. How to use Three-layer architecture in Spring MVC web applications.