One of the ways of working with the Spring framework is by using an form of the N-tier architecture pattern. We can conceptualize it like this:
When our frontend framework sends a request to the server it reaches our controller. The controller is annotated with @RestController. This separtes the UI from the backend logic. This makes it possible to sue the same controller for a webfrontend, a desktop frontend or a CLI (command line interface).
The controller has a service component autowired which can help him return the request.
A service component is annotated with @Service. It it interacts with data through an autowired repository and contains the businesses logic. Another name for this layer is the business layer.
A repository component is annotated with @Repository. It interacts with the chosen way to persist data. Another name for this layer is the data access layer.
We can replace classes in any of the layers with new classes that confirm to the same interface. This makes managing an application a lot easier, because you are dealing with the same level of abstraction all the time. Imagine dealing with database interaction in the same class as your UI. It’s a great way to create spaghetti code.