Solve Cross Cutting
Concerns with Aspect
Oriented programming (AOP)
Siva Prasad Rao Janapati
http://smarttechie.org
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?
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?
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
AOP at a glance
ADVICE
APPLICATION CODE EXECUTION
ADVICEADVICE
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.
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
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.
Spring AOP - Lets see in action
https://github.com/2013techsmarts/spring-aop
Questions

Solve cross cutting concerns with aspect oriented programming (aop)

  • 1.
    Solve Cross Cutting Concernswith Aspect Oriented programming (AOP) Siva Prasad Rao Janapati http://smarttechie.org
  • 2.
    Implement method execution metrics. Hey, Can youadd 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 areparts 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 rescueme? 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 aglance ADVICE APPLICATION CODE EXECUTION ADVICEADVICE
  • 6.
    Dive into AOPterminology ❏ 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 AOPimplementers 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
  • 10.