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

Course Content

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

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. Let's go over each one in sequence.

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

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

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

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.

Everything was clear?

Section 2. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt