0% found this document useful (0 votes)
34 views8 pages

SpringBoot AOP

The document discusses Aspect-Oriented Programming (AOP) in Spring Boot, emphasizing its role in managing cross-cutting concerns like logging and security across different application layers. It defines key terminologies such as aspects, pointcuts, join points, and advices, detailing their functions and types. Additionally, it highlights the benefits of AOP, including improved code modularity and maintainability, and introduces the Spring Boot Starter AOP dependency for implementation.

Uploaded by

shail.022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

SpringBoot AOP

The document discusses Aspect-Oriented Programming (AOP) in Spring Boot, emphasizing its role in managing cross-cutting concerns like logging and security across different application layers. It defines key terminologies such as aspects, pointcuts, join points, and advices, detailing their functions and types. Additionally, it highlights the benefits of AOP, including improved code modularity and maintainability, and introduces the Spring Boot Starter AOP dependency for implementation.

Uploaded by

shail.022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SPRING BOOT AOP

*Réussir la transformation. Ensemble.

Delivering Transformation. Together.


2 Nom de la présentation
C2 - Restricted
SPRING BOOT AOP, WHY AOP ?
Software application is developed with multiple layers, example as below :
• Web Layer: It exposes the services using the REST or web application.
• Business Layer: It implements the business logic of an application.
• Data Layer: It implements the persistence logic of the application.

The responsibility of each layer is different, however there are a few common aspects that apply to all layers are Logging, Security, validation,
caching, etc. These common aspects are called cross-cutting concerns.

If we implement these concerns in each layer separately, the code becomes difficult to maintain. To overcome this problem, Aspect-Oriented
Programming (AOP) provides a solution to implement cross-cutting concerns.
• Implement the cross-cutting concern as an aspect.
• Define pointcuts to indicate where the aspect has to be applied.
It ensures that the cross-cutting concerns are defined in one cohesive code component.

Cross-cutting concern - The cross-cutting concern is a concern that we want to implement in multiple places in an application. It affects the
entire application.

3
C2 - Restricted
SPRING BOOT AOP - TERMINOLOGY
Aspect: An aspect is a module that encapsulates advice and pointcuts and provides cross-cutting An application can have any number of aspects. We
can implement an aspect using regular class annotated with @Aspect annotation.

• Pointcut: A pointcut is an expression that selects one or more join points where advice is executed. We can define pointcuts
using expressions or patterns. It uses different kinds of expressions that matched with the join points. In Spring Framework, AspectJ pointcut
expression language is used.

• Join point: A join point is a point in the application where we apply an AOP aspect. Or it is a specific execution instance of an advice. In AOP, join point
can be a method execution, exception handling, changing object variable value, etc.

• Advice: The advice is an action that we take either before or after the method execution. The action is a piece of code that invokes during the
program execution. There are five types of advices in the Spring AOP framework: before, after, after-returning, after-throwing, and around
advice. Advices are taken for a particular join point. We will discuss these advices further in this section.

• Target object: An object on which advices are applied, is called the target object. Target objects are always a proxied It means a subclass is created at
run time in which the target method is overridden, and advices are included based on their configuration.

• Weaving: It is a process of linking aspects with other application types. We can perform weaving at run time, load time, and compile time.

4
C2 - Restricted
SPRING BOOT AOP - BENEFITS
AOP (Aspect-Oriented Programming) is a programming pattern that increases modularity by allowing the separation of the cross-cutting concern. These
cross-cutting concerns are different from the main business logic. Additional behavior can be added to the existing code without modification of the code
itself.

Benefits of aspects:
• First, the logic for each concern is now in one place instead of scattered all over the codebase.
• Second, the business modules only contain code for their primary concern. The secondary concern has been moved to the aspect.
Advice: It is an implementation of cross-cutting concerns.

Types of AOP Advices, There are five types of AOP advices as follows:
Before Advice: An advice that executes before a join point, is called before advice. We use @Before annotation to mark an advice as Before advice.
After Advice: An advice that executes after a join point, is called after advice. We use @After annotation to mark an advice as After advice.
Around Advice: An advice that executes before and after of a join point, is called around advice.
After Throwing Advice: An advice that executes when a join point throws an exception.
After Returning Advice: An advice that executes when a method executes successfully.

5
C2 - Restricted
SPRING BOOT AOP – 5 TYPES OF AOP ADVICES
Before Advice: An advice that executes before a join point, is called before advice. We use @Before annotation to mark an advice as Before advice.
@Before(value = "execution(* [Link].*(..)) and args(empId, fname, sname)")
public void beforeAdvice(JoinPoint joinPoint, String empId, String fname, String sname) {
[Link]("Before method:" + [Link]()); }
After Advice: An advice that executes after a join point, is called after advice. We use @After annotation to mark an advice as After advice.
@After(value = "execution(* [Link].*(..)) and args(empId, fname, sname)")
public void afterAdvice(JoinPoint joinPoint, String empId, String fname, String sname) {
[Link]("After method:" + [Link]());}
Around Advice: An advice that executes before and after of a join point, is called around advice.
@Around(value= "logDisplayingBalance()")
public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable {
[Link]("The aroundAdvice() before invokation of the method " + [Link]().getName() + " method"); }
After Throwing Advice: An advice that executes when a join point throws an exception.
@AfterThrowing(value="execution(* [Link].*(..))",throwing="ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
[Link]("After Throwing exception in method:"+[Link]()); }
After Returning Advice: An advice that executes when a method executes successfully.
@AfterReturning(value="execution(* [Link].*(..))",returning="account")
public void afterReturningAdvice(JoinPoint joinPoint, Account account){
[Link]("After Returing method:"+[Link]()); }
6
C2 - Restricted
SPRING BOOT AOP - STARTER
Spring Boot Starter AOP
Spring Boot Starter AOP is a dependency that provides Spring AOP and AspectJ. Where AOP provides basic AOP capabilities while the AspectJ provides a
complete AOP framework.
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>[Link]</version>
</dependency>

Aspect: A code unit that encapsulates pointcuts, advices, and attributes.


Pointcut: It defines the set of entry points in which advice is executed.
Advice: It is an implementation of cross-cutting concerns.
Waver: It constructs code (source or object) with advice.

7
C2 - Restricted
Delivering Transformation. Together.

8 Nom de la présentation
C2 - Restricted

You might also like