2. Implement
method
execution
metrics.
Hey, Can you add
loggers for our
legacy code?
Add role
check(Security)
for all our API
methods.
❏ Caching
❏ Monitoring
❏ Error detection
❏ Data validation
❏ Internationalization and Localization
❏ Transaction processing
What if you come across below scenarios?
3. Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system.
❏ Scattered, or duplicated code
❏ Loss of modularity
❏ Not easy to refactor
❏ Not easy to understand and
troubleshoot
❏ Code maintainability becomes
nightmare
What if you don’t address cross-cutting
concerns properly?
What is a cross-cutting concern?
4. Who will rescue me?
Aspect Oriented Programming (AOP)
Aspect-oriented programming aims to encapsulate cross-cutting
concerns into aspects (common feature) to retain modularity.
❏ Clean isolation and reuse of code
❏ Code modularity
❏ Simplified maintenance
5. AOP at a glance
ADVICE
APPLICATION CODE EXECUTION
ADVICEADVICE
6. Dive into AOP terminology
❏ Advice – What is the additional logic to execute when
Pointcut is evaluated as true.
❏ Pointcut – Pointcut is the expression used to define when
the call to a method is intercepted.
❏ Aspect – Pointcut + Advice is called Aspect.
7. Dominant Java AOP implementers
AOP
JBossSpringAspectJ
AOP not only for Java, many languages implemented this concept.
https://en.wikipedia.org/wiki/Aspect-oriented_programming#Implementations
8. Spring AOP - Types of advices
Types of Advices
Before After After Returning Around After Throwing
❏ Before – Advice is called “Before” executing the intended method.
❏ After – Advice is called “After” executing the intended method without success/failure.
❏ After Returning – Advice is called after successful completion of intended method execution.
❏ Around – Advice is called before and after the intended method execution.
❏ After Throwing – Advice is called after the intended method throws runtime exception.
9. Spring AOP - Lets see in action
https://github.com/2013techsmarts/spring-aop