Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Three-Level Architecture | Spring Boot Basics
Spring Boot Backend
course content

Contenido del Curso

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

book
Three-Level Architecture

This is done to separate specific logic into different classes/packages, rather than writing everything in a single class.

The order in which a request is processed is ControllerServiceRepository. After that, the response is returned in reverse order: Repository -> Service -> Controller. We'll start the implementation with the Repository layer.

Repository Level

This is the lowest layer, where we receive processed data from the Service layer and store it in our database.

These classes are marked with the @Repository annotation to be added to the Spring context.

java

Main

copy
1234
@Repository public class RepositoryLevel { // connect DB }

Example of Using the Repository

Service Level

This is where the core logic of the application happens. In services, we handle or modify data before passing it to the repository layer.

For services, we use the @Service annotation, which declares the class as a service that contains the business logic.

java

Main

copy
1234567
@Service public class ServiceLevel { public void logic() { //TODO: write business logic } }

Example of Using the Service

Controller Level

This layer handles the initial interaction between the client and the server. All requests sent from the client arrive here, and it is responsible for receiving the data provided by the client.

It processes incoming HTTP requests and returns HTTP responses. Controllers act as a "bridge" between the client and the business logic.

At this level, there are two annotations used to designate a class as a controller:

  • @RestController: Declares a class as a REST controller, which handles HTTP requests and returns data in JSON format;

  • @Controller: Declares a class as an MVC controller, which handles requests and returns views (e.g., HTML).

java

Main

copy
12345678
@Controller public class ControllerLevel { @GetMapping("/root") public String getPage() { return "template"; } }

The @GetMapping annotation specifies the URL for a given request. This means that we add the specified path /root to the domain, and in return, we receive the corresponding page.

Example of Using the Controller

The Thymeleaf Addiction From the Video

Here's the link to the Thymeleaf dependency in the Maven repository.

But What Happens if you Don't Follow this Approach?

Well, technically, nothing. Even if you write all the business logic in the controller, connect to the database there, and return the response to the client from the same place, everything will work the same.

However, you're unlikely to remember what you wrote there after a few weeks, because all the application logic will be in one place, which is incredibly inconvenient.

Summary

The three-tier architecture provides a clear separation of concerns between the layers of controllers, services, and repositories, making the development process more organized and easier to maintain.

Each layer focuses on its specific task, which simplifies both the workflow and project management.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt