Chapter III SpringBoot - Part 5 - Service
Chapter III SpringBoot - Part 5 - Service
SERVICE LAYER
SPRING BOOT / SERVICE LAYER
§ Service Layer: the service layer implements all the business logic.
§ It managing the interactions between the controller (which handles incoming HTTP
requests) and the data access layer (which interacts with the database or any other
data source).
§ It consists of service components that use services provided by data access layers.
2
SPRING BOOT / SERVICE LAYER ROLE
§ Business Logic Implementation: The service layer is where you put the actual
business logic of your application. This can involve processing data, applying
algorithms, performing validations, and making decisions based on the data
received.
§ Abstraction from Data Access: The service layer abstracts the data access
layer from the controller. It encapsulates the interactions with the database
or any other data source.
3
SPRING BOOT / SERVICE LAYER ROLE
§ Service Interface Definition: It defines the contract (i.e., the methods and
their signatures) that the service provides. This interface can be used for
dependency injection, allowing for easy substitution of different
implementations (e.g., in testing or when switching between data sources).
4
SPRING BOOT / SERVICE LAYER ROLE
§ Validation: The service layer can be responsible for input validation. This
5
SPRING BOOT / SERVICE CLASS
6
SPRING BOOT / SERVICE CLASS
exceptions thrown by the data access layer can be caught and possibly
translated into meaningful business-level exceptions. This way, the service
layer can provide a more user-friendly response.
stubbing dependencies, you can focus on testing the business logic without
worrying about the details of data access or HTTP requests.
7
SPRING BOOT / SERVICE CLASSES
§ Service Classes: In the context of the business layer, service classes are specialized
modules or components responsible for executing specific tasks related to the business
logic.
§ These classes contain methods that encapsulate the functionality required for a
particular aspect of the application.
§ In spring boot, the annotation @Service is used to indicate that a class is a service
component.
8
SPRING BOOT / SERVICE CLASSES
9
SPRING BOOT / @TRANSACTIONAL ANNOTATION
§ Transactions: In a database, a transaction is a sequence of operations that are executed
as a single unit of work. This means that either all of the operations succeed, and the
data is permanently changed in the database, or none of them succeed, and the
database remains unchanged.
10
SPRING BOOT / @TRANSACTIONAL ANNOTATION
Class-level:
§ When @Transactional is placed at the class level, it means that all public methods in that
class are transactional.
§ Each
method starts a new transaction, and the transaction is committed when the
method finishes executing.
11
SPRING BOOT / @TRANSACTIONAL ANNOTATION
Method-level:
§ When@Transactional is placed directly on a method, it means that only that specific
method is transactional.
12
SPRING BOOT / @TRANSACTIONAL ANNOTATION
Transaction propagation: defines the behavior of an existing transaction when a method
annotated with @Transactional is called.
In other words, it decides what happens when a method that's already participating in a
transaction calls another method that's also annotated with @Transactional.
There are several propagation options available:
REQUIRED (default):
§ If there is an existing transaction, the method will join it.
§ If there's no existing transaction, a new one will be created.
REQUIRES_NEW:
§ A new transaction will always be created, even if there's an existing transaction.
§ Ifthere's an ongoing transaction, it will be suspended while the new transaction is
created and executed.
13
SPRING BOOT / @TRANSACTIONAL ANNOTATION
Rolling Back Transactions: You can specify conditions under which a transaction should
be rolled back using rollbackFor and noRollbackFor attributes:
14
SPRING BOOT / @ASYNC ANNOTATION
§ @Async annotation: in Spring is used to indicate that a particular method should be
executed asynchronously, meaning it will run in a separate thread and not block the
calling thread.
§ This can be very useful for tasks that can be performed independently of the main
application flow, such as sending emails, performing background computations, etc.
15
SPRING BOOT / @ASYNC ANNOTATION
Enable Async Support for spring boot application.
16
SPRING BOOT / @ASYNC ANNOTATION
Add @Async Annotation to a method
17