0% found this document useful (0 votes)
19 views

Chapter III SpringBoot - Part 5 - Service

The document discusses the role and implementation of the service layer in Spring Boot applications. The service layer implements business logic, manages interactions between controllers and data access layers, and consists of service components that use data access services. Key responsibilities of the service layer include business logic, abstracting data access, defining service interfaces, transaction management, validation, security, caching, and error handling. Service classes contain application business logic and are annotated with @Service. The @Transactional annotation manages transactions at the class or method level. Asynchronous methods are annotated with @Async.

Uploaded by

Mary Norssine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Chapter III SpringBoot - Part 5 - Service

The document discusses the role and implementation of the service layer in Spring Boot applications. The service layer implements business logic, manages interactions between controllers and data access layers, and consists of service components that use data access services. Key responsibilities of the service layer include business logic, abstracting data access, defining service interfaces, transaction management, validation, security, caching, and error handling. Service classes contain application business logic and are annotated with @Service. The @Transactional annotation manages transactions at the class or method level. Asynchronous methods are annotated with @Async.

Uploaded by

Mary Norssine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1

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).

§ Aggregation and Orchestration: Services can aggregate data from multiple

sources or perform orchestration tasks where multiple operations need to be


coordinated.

4
SPRING BOOT / SERVICE LAYER ROLE

§ Transaction Management: In many cases, the service layer is responsible for

managing transactions. This ensures that multiple operations either succeed


together or fail together, providing data integrity.

§ Validation: The service layer can be responsible for input validation. This

includes checking the validity and integrity of data before processing it


further. Validation helps ensure that the data meets the required criteria and
prevents potential errors or security issues.

5
SPRING BOOT / SERVICE CLASS

§ Security: Depending on the application's requirements, the service layer can

also handle security-related operations, such as authorization checks.

§ Caching and Optimization: The service layer can implement caching

mechanisms to improve performance by reducing the need to access the data


source repeatedly for the same information.

6
SPRING BOOT / SERVICE CLASS

§ Error Handling and Exception Translation: The service layer is where

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.

§ Testing: The service layer is relatively easy to test in isolation. By mocking or

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.

§ @Transactional: This annotation is used to specify the scope of a single database


transaction. It can be applied at the class level or at the method level.

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.

1. Enable Async Support for spring boot application.

2. Add @Async Annotation to a method.

15
SPRING BOOT / @ASYNC ANNOTATION
Enable Async Support for spring boot application.

16
SPRING BOOT / @ASYNC ANNOTATION
Add @Async Annotation to a method

17

You might also like