際際滷

際際滷Share a Scribd company logo
AOP (Aspect-Oriented Programming)
AOP is a programming paradigm that helps separate cross-cutting concerns (like logging,
security, transaction management, etc.) from the core business logic. This improves modularity
and reduces code duplication.
 Key Concepts of AOP
1. Aspect  A modular component that implements a cross-cutting concern.
2. Pointcut  Defines where an aspect should be applied (e.g., before or after a method).
3. Advice  The actual code that runs at a pointcut (before, after, around).
 Example in Java with Spring AOP
``
Here, the LoggingAspect intercepts the execution of all methods in com.example.service
and logs a message before they are executed.
 Advantages of AOP:
 Reduces code duplication
 Better separation of concerns
 Easier maintenance
AOP
Programming technique based on concept of an aspect.
Aspect encapsulates cross-cutting logic.
"concerns" means logic / functionality.
Aspect can be reused at multiple location.
Same aspect/class .. applied based on configuration.
Apply the proxy design pattern
4. Joinpoint  A specific point where an aspect can be applied (e.g., method calls, field
access).
5. Weaving  The process of injecting aspects into the main code (at compile-time, load-time,
or runtime).
@Aspect @Component public class LoggingAspect { @Before("execution(*
com.example.service.*.*(..))") public void logBeforeMethodExecution() {
System.out.println("Method executed: Logging before execution."); } }
code for aspect is defined in a single class:
1) much better than being scattered everywhere.
2) Promotes code reuse and easier to change.
Businees code in your applicaion is cleaner:
1) Only applies to business functionnality: addAccount.
2) Reduces code complexity.
ADDITIONAL AOP USE CASES:
Advices Types:
Before advice: run before the method.
After finally advice: run after method (finally).
after returning advice : run after method (success execution).
Most common : logging, security, transactions
Audit logging : who, where, when, what.
Exception handling : log exception and notify Devops team via sms/mail.
API management : how many times has a method been called user.
Advantages:
- reusable modules, resolve code tangling, resolve code scatter, applied selectively based
on configuration.
AOP TERMINOLOGY
- aspect : module of code for a cross-cutting concern(logging, security, transaction...).
- Advice : what action is taken and when it should be applied.
- join point : when to apply code during program execution.
- pointcut : A predicate expression for where advice should be applied.
after throwing advice : run after method (if exception thrown).
around advice : run before and after method.
Weaving:
connecting aspects to target objects to create an advised object.
different types of weaving:
compile-time, load-time, run-time.
AOP Frameworks
Spring AOP, AspectJ.
Spring AOP : method-level join points, Run-time code weaving (slower that aspectJ).
Start with Advices_:
Before Advice : run before a method.(@Before)
AOP POINTCUT
@Ascpect
public class mydemo{
@Before("execution(public void assAccount())")
public void beforeAddClass(){
....;
}
}
A predicate expression for where advice should be applied.
The pattern is optional if it has ?.
Parameter pattern wildcards:
(): matches a method with no arguments.
() : matches a method with one argument of any type.
(..) : matches a method with 0 or more argumenets of any type.
AOP (Aspect-Oriented Programming) spring boot
java code:
@Aspect
@Component
public class MyloggingAspect {
@Pointcut("execution(* org.example.ap.Dao.*.*(..))")
public void forDaoPackage() {}
//@Before("execution(public void
org.example.ap.Dao.AccountDao.addAccount())")
//@Before("execution(public void updateAccount())")
//@Before("execution(public void add*())") //@Before("execution(void add*
())") //@Before("execution(* add*(int,..))") @Before("forDaoPackage()")
public void beforeAddAccountAdvice() {
System.out.println("n=====>>> Executing @Before advice on
Combining pointcuts:
How to control order of advices being applied:
solution is:
1) Refactor: Place advice in separate aspect.
2) Control order on aspects using the @Order annotation
3) Guarantees order of when Aspects are applied.
addAccount()");
}
}
Reading method argument with joinPoints:
More Advices:
@AfterReturning Advice
After successful execution.
after throwing an exception.
in all after cases
before and after.

More Related Content

Similar to AOP (Aspect-Oriented Programming) spring boot (20)

Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
Spring AOP in Nutshell
Spring AOP in Nutshell Spring AOP in Nutshell
Spring AOP in Nutshell
Onkar Deshpande
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka
Aspect Oriented Programming and MVC with Spring Framework
Aspect Oriented Programming and MVC with Spring FrameworkAspect Oriented Programming and MVC with Spring Framework
Aspect Oriented Programming and MVC with Spring Framework
Massimiliano Dess狸
Spring AOP
Spring AOPSpring AOP
Spring AOP
Lhouceine OUHAMZA
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
Sujit Kumar
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
Amir Kost
Code Quality Management iOS
Code Quality Management iOSCode Quality Management iOS
Code Quality Management iOS
Arpit Kulsreshtha
Aspect Oriented Programming: Hidden Toolkit That You Already Have
Aspect Oriented Programming: Hidden Toolkit That You Already HaveAspect Oriented Programming: Hidden Toolkit That You Already Have
Aspect Oriented Programming: Hidden Toolkit That You Already Have
Salesforce Engineering
C question
C questionC question
C question
Kuntal Bhowmick
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
Shreya Chatterjee
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
Katy Slemon
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
Jerry Kurian
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1  Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
securityxploded
AOP
AOPAOP
AOP
Joshua Yoon
WoMakersCode 2016 - Shit Happens
WoMakersCode 2016 -  Shit HappensWoMakersCode 2016 -  Shit Happens
WoMakersCode 2016 - Shit Happens
Jackson F. de A. Mafra
Node.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniquesNode.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniques
Manuel Eusebio de Paz Carmona
Spring framework AOP
Spring framework  AOPSpring framework  AOP
Spring framework AOP
Anuj Singh Rajput
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC TeamAOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
Thuy_Dang
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
Spring AOP in Nutshell
Spring AOP in Nutshell Spring AOP in Nutshell
Spring AOP in Nutshell
Onkar Deshpande
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka
Aspect Oriented Programming and MVC with Spring Framework
Aspect Oriented Programming and MVC with Spring FrameworkAspect Oriented Programming and MVC with Spring Framework
Aspect Oriented Programming and MVC with Spring Framework
Massimiliano Dess狸
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
Sujit Kumar
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
Amir Kost
Code Quality Management iOS
Code Quality Management iOSCode Quality Management iOS
Code Quality Management iOS
Arpit Kulsreshtha
Aspect Oriented Programming: Hidden Toolkit That You Already Have
Aspect Oriented Programming: Hidden Toolkit That You Already HaveAspect Oriented Programming: Hidden Toolkit That You Already Have
Aspect Oriented Programming: Hidden Toolkit That You Already Have
Salesforce Engineering
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
Shreya Chatterjee
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
Katy Slemon
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
Jerry Kurian
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1  Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
Advanced Malware Analysis Training Session 2 - Botnet Analysis Part 1
securityxploded
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC TeamAOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
Thuy_Dang

Recently uploaded (11)

L 19 Harmony in Nature_ Value Education .ppt
L 19 Harmony in  Nature_ Value Education .pptL 19 Harmony in  Nature_ Value Education .ppt
L 19 Harmony in Nature_ Value Education .ppt
AkashV75
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdfTi畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
quyn641hd
The paradox of Perfection - why performance pressure counteracts leadership
The paradox of Perfection - why performance pressure counteracts leadershipThe paradox of Perfection - why performance pressure counteracts leadership
The paradox of Perfection - why performance pressure counteracts leadership
Wico Mulder
VivekaChudamani-of-Sri-Shankaracharya.pdf
VivekaChudamani-of-Sri-Shankaracharya.pdfVivekaChudamani-of-Sri-Shankaracharya.pdf
VivekaChudamani-of-Sri-Shankaracharya.pdf
DrPAnbhazhagan
How Yoga Nidra Teacher Training Prepares You for Spiritual Growth
How Yoga Nidra Teacher Training Prepares You for Spiritual GrowthHow Yoga Nidra Teacher Training Prepares You for Spiritual Growth
How Yoga Nidra Teacher Training Prepares You for Spiritual Growth
David Cook
The-Science-of-Goal-Setting-and-Achieving-Success
The-Science-of-Goal-Setting-and-Achieving-SuccessThe-Science-of-Goal-Setting-and-Achieving-Success
The-Science-of-Goal-Setting-and-Achieving-Success
Ozias Rondon
Story, Life Essences Intervention Program
Story, Life Essences Intervention ProgramStory, Life Essences Intervention Program
Story, Life Essences Intervention Program
Omar Al Naqa
Mastering-Emotional-Intelligence.Presentation
Mastering-Emotional-Intelligence.PresentationMastering-Emotional-Intelligence.Presentation
Mastering-Emotional-Intelligence.Presentation
Ozias Rondon
How I Envision Pakistan by the year 2035.pdf
How I Envision Pakistan by the year 2035.pdfHow I Envision Pakistan by the year 2035.pdf
How I Envision Pakistan by the year 2035.pdf
heyitsyouhere123
L 18 Vision for the Universal Human Order v2.ppt
L 18 Vision for the Universal Human Order v2.pptL 18 Vision for the Universal Human Order v2.ppt
L 18 Vision for the Universal Human Order v2.ppt
AkashV75
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-ProMind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Ozias Rondon
L 19 Harmony in Nature_ Value Education .ppt
L 19 Harmony in  Nature_ Value Education .pptL 19 Harmony in  Nature_ Value Education .ppt
L 19 Harmony in Nature_ Value Education .ppt
AkashV75
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdfTi畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
Ti畉ng anh t畛 h畛c -10 WEEKS - VOCABULARY .pdf
quyn641hd
The paradox of Perfection - why performance pressure counteracts leadership
The paradox of Perfection - why performance pressure counteracts leadershipThe paradox of Perfection - why performance pressure counteracts leadership
The paradox of Perfection - why performance pressure counteracts leadership
Wico Mulder
VivekaChudamani-of-Sri-Shankaracharya.pdf
VivekaChudamani-of-Sri-Shankaracharya.pdfVivekaChudamani-of-Sri-Shankaracharya.pdf
VivekaChudamani-of-Sri-Shankaracharya.pdf
DrPAnbhazhagan
How Yoga Nidra Teacher Training Prepares You for Spiritual Growth
How Yoga Nidra Teacher Training Prepares You for Spiritual GrowthHow Yoga Nidra Teacher Training Prepares You for Spiritual Growth
How Yoga Nidra Teacher Training Prepares You for Spiritual Growth
David Cook
The-Science-of-Goal-Setting-and-Achieving-Success
The-Science-of-Goal-Setting-and-Achieving-SuccessThe-Science-of-Goal-Setting-and-Achieving-Success
The-Science-of-Goal-Setting-and-Achieving-Success
Ozias Rondon
Story, Life Essences Intervention Program
Story, Life Essences Intervention ProgramStory, Life Essences Intervention Program
Story, Life Essences Intervention Program
Omar Al Naqa
Mastering-Emotional-Intelligence.Presentation
Mastering-Emotional-Intelligence.PresentationMastering-Emotional-Intelligence.Presentation
Mastering-Emotional-Intelligence.Presentation
Ozias Rondon
How I Envision Pakistan by the year 2035.pdf
How I Envision Pakistan by the year 2035.pdfHow I Envision Pakistan by the year 2035.pdf
How I Envision Pakistan by the year 2035.pdf
heyitsyouhere123
L 18 Vision for the Universal Human Order v2.ppt
L 18 Vision for the Universal Human Order v2.pptL 18 Vision for the Universal Human Order v2.ppt
L 18 Vision for the Universal Human Order v2.ppt
AkashV75
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-ProMind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Mind-Mapping-How-to-Organize-Your-Ideas-Like-a-Pro
Ozias Rondon

AOP (Aspect-Oriented Programming) spring boot

  • 1. AOP (Aspect-Oriented Programming) AOP is a programming paradigm that helps separate cross-cutting concerns (like logging, security, transaction management, etc.) from the core business logic. This improves modularity and reduces code duplication. Key Concepts of AOP 1. Aspect A modular component that implements a cross-cutting concern. 2. Pointcut Defines where an aspect should be applied (e.g., before or after a method). 3. Advice The actual code that runs at a pointcut (before, after, around).
  • 2. Example in Java with Spring AOP `` Here, the LoggingAspect intercepts the execution of all methods in com.example.service and logs a message before they are executed. Advantages of AOP: Reduces code duplication Better separation of concerns Easier maintenance AOP Programming technique based on concept of an aspect. Aspect encapsulates cross-cutting logic. "concerns" means logic / functionality. Aspect can be reused at multiple location. Same aspect/class .. applied based on configuration. Apply the proxy design pattern 4. Joinpoint A specific point where an aspect can be applied (e.g., method calls, field access). 5. Weaving The process of injecting aspects into the main code (at compile-time, load-time, or runtime). @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBeforeMethodExecution() { System.out.println("Method executed: Logging before execution."); } }
  • 3. code for aspect is defined in a single class: 1) much better than being scattered everywhere. 2) Promotes code reuse and easier to change. Businees code in your applicaion is cleaner: 1) Only applies to business functionnality: addAccount. 2) Reduces code complexity. ADDITIONAL AOP USE CASES: Advices Types: Before advice: run before the method. After finally advice: run after method (finally). after returning advice : run after method (success execution). Most common : logging, security, transactions Audit logging : who, where, when, what. Exception handling : log exception and notify Devops team via sms/mail. API management : how many times has a method been called user. Advantages: - reusable modules, resolve code tangling, resolve code scatter, applied selectively based on configuration. AOP TERMINOLOGY - aspect : module of code for a cross-cutting concern(logging, security, transaction...). - Advice : what action is taken and when it should be applied. - join point : when to apply code during program execution. - pointcut : A predicate expression for where advice should be applied.
  • 4. after throwing advice : run after method (if exception thrown). around advice : run before and after method. Weaving: connecting aspects to target objects to create an advised object. different types of weaving: compile-time, load-time, run-time. AOP Frameworks Spring AOP, AspectJ. Spring AOP : method-level join points, Run-time code weaving (slower that aspectJ). Start with Advices_: Before Advice : run before a method.(@Before) AOP POINTCUT @Ascpect public class mydemo{ @Before("execution(public void assAccount())") public void beforeAddClass(){ ....; } }
  • 5. A predicate expression for where advice should be applied. The pattern is optional if it has ?.
  • 6. Parameter pattern wildcards: (): matches a method with no arguments. () : matches a method with one argument of any type. (..) : matches a method with 0 or more argumenets of any type.
  • 8. java code: @Aspect @Component public class MyloggingAspect { @Pointcut("execution(* org.example.ap.Dao.*.*(..))") public void forDaoPackage() {} //@Before("execution(public void org.example.ap.Dao.AccountDao.addAccount())") //@Before("execution(public void updateAccount())") //@Before("execution(public void add*())") //@Before("execution(void add* ())") //@Before("execution(* add*(int,..))") @Before("forDaoPackage()") public void beforeAddAccountAdvice() { System.out.println("n=====>>> Executing @Before advice on
  • 9. Combining pointcuts: How to control order of advices being applied: solution is: 1) Refactor: Place advice in separate aspect. 2) Control order on aspects using the @Order annotation 3) Guarantees order of when Aspects are applied. addAccount()"); } }
  • 10. Reading method argument with joinPoints:
  • 13. after throwing an exception.
  • 14. in all after cases