The flow goes in Spring Boot

Tram Ho

1. Two familiar patterns

The architecture of Spring Boot application is built on 2 MVC models and 3 layers model.

1.1. Three tier model

This is the source code organization model that is very popular in Spring Boot. Specifically, the application is divided into 3 floors (tier or layer) as follows:

  • The presentation layer: This layer interacts with the user, either by View, Controller (in MVC) or API (if any).
  • Business logic layer: Contains the entire logic of the program, most of the code is located here
  • Data access layer: Interacts with database, returns results to business logic layer

In Spring Boot, the two layers below have representative components:

  • Service: contains the business logic code
  • Repository: represents the data access layer

As for the presentation layer, we will continue to discuss the MVC model below.

1.2. MVC model

Since Spring Boot is just a Spring wrapper, we still use implicitly other Spring modules below, such as Spring MVC. And of course when using Spring MVC, it will follow MVC model.

MVC pattern is too popular, so I don’t mention much. Specifically, it divides the presentation layer into 3 parts:

  • Model: the data structures of the entire program, can represent the state of the application
  • View: interface layer, used to display output for users to view and interact with
  • Controller: connects between Model and View, controls the flow of data

Data from Model via Controller is then sent to View for display. And vice versa, when there is a new request from the View, the Controller will make changes to the Model’s data.

However, MVC only describes the flow of data, it does not specify where the code is located (in Model, View or Controller), then what type of storage the Model to the database, … Therefore, for the application As complete as Spring Boot, it is necessary to combine both MVC and 3-tier models together.

2. Applying to Spring Boot, how?

2.1. Important ingredients

Combining the two models, we have a complete Spring Boot application, including the following components:

  • Controller: returns View (contains available data, HTML page form), or Model representation as API for View (View is written in React, Vue, or Angular).
  • Service: contains computation and processing code. When the Controller requests, the corresponding Service will receive and give data returned to the Controller (return Model). Controller will send to View as above.
  • Repository: Service can also interact with other services, or use Repository to call DB. Repository is a guy that directly interacts, reads and writes data in DB and pays the service.

So where do the Model and View go? I will explain as follows:

  • Models are simply the objects that the Service has calculated and returned to the Controller.
  • View has 2 types, one is the traditional form that returns 1 piece of HTML with data. At this time, Controller will pass data to View and return (Spring MVC has JSP or template engine like Thymeleaf to do it).
  • View type 2 is a separate View (not a Spring boot project). Usually found in APIs. View will be written separately in React, Angular, … Controller will pass Model data through API to View, and also receive requests via API too.

2.2. Flow diagrams

To better understand the components in Spring Boot, let’s take a look at the flow diagram and their interactions. This can be a bit confusing, but you need to know it carefully.

The above diagram will look clockwise:

  • First, the user will go to View to view and interact
  • When the user starts to load data (for example, click the Reload button), then a request from the View is sent to the Controller (like “eh, give me a list of users with”)
  • Controller receives the request, begins to ask Mr. Service (in the code is called the method of Service)
  • Service receives the request from Controller, for simple code it can be calculated and returned. But for operations that need to touch the database, the Service must call Repository to get data in the DB
  • Repository receives request from Service, will manipulate DB. Data retrieved in DB is mapped by the ORM system (like JPA or Hibernate) into objects (in Java). These objects are called Entity.

And now it will be the reverse return to the user:

  • Service receives Entities returned by Repository, modifying it. The transformation here is being able to perform calculations, add or remove fields, … and finally turn Entity into Model. The model will be returned to Controller.
  • Controller receives the Model, it will return to View. There are two ways, one is to use the template engine to pass the Model data into the HTML page, and then return the HTML department (already with the data) to the client. Method 2 is to send via API, View automatically parses and displays accordingly (depending on View).
  • When the View is displayed, the user will see a list of users appearing on the website.

Some great tips for organizing your flow well:

  • Keep Controller as little code as possible. Because the Controller is just an intermediary connecting through the connection, it should not contain a lot of code. Instead, it should be put in the Service.
  • Service should be separated clearly. Do not let 1 service do many jobs, should separate many Services.

2.3. Code examples

The above components can interact, call each other, that is nothing but dependency injection mechanism in Spring. As follows:

  • The Controller is injected with necessary Services
  • The Service is injected with necessary Repositories

I will code a simple example, which is to display the User list in the DB. Code outside the default file will have

Above is the code of 3 components Controller, Service and Repository. We need to have two more classes representing the data, which are Entity (to map in the DB) and Model (to turn Entity into an appropriate form to return, for example Entity contains a password that is always broken..

From Entity to Model I can use the mapping library, but I will not use it but merely copy it.


Done, today’s post is quite long, difficult but important. Where in the code is difficult to understand, just comment below, I will answer all. Thanks.

Share the news now

Source : Viblo