AOP

1. What is the concept of AOP?

AOP is Aspect Oriented Programming. AOP is a type of programming that aims to help with separation of cross-cutting concerns to increase modularity; it implies declaring an aspect class that will alter the behavior of base code, by applying advices to specific join points, specified by pointcuts.

2. What problems does it solve? What two problems arise if you don’t solve a cross cutting concern via AOP?

Aims to help with separation of cross-cutting concerns to increase modularity.

  1. Avoid tangling: mixing business logic and cross cutting concerns
  2. Avoid scattering: code duplication in several modules

Code tangling and code scattering are two issues that come together when base code needs to be executed under certain conditions. Code tangling means that the same component is injected into many others, thus leading to significant dependencies coupling components together. Code scattering means duplication, because you can do the same thing in only one way. The resulting solution is difficult to extend and to maintain, harder to understand, and a pain to debug.

3. What is a cross cutting concern? 

cross-cutting concern is a functionality that is tangled with business code, which usually cannot be separated from the business logic.

4. Name three typical cross cutting concerns.

  • Auditing
  • Security
  • Transaction management

Other examples of cross-cutting concerns

  • Logging
  • Caching
  • Internationalization
  • Error detection and correction
  • Memory management
  • Performance monitoring
  • Measuring statistics
  • Synchronization

Not a cross-cutting concerns

  • connecting to the database


Two popular AOP libraries

  1. AspectJ is the original AOP technology which aims to provide complete AOP solution. More robust, more complicated. It uses three different types of weaving:
    1. Compile-time weaving
    2. Post-compile weaving
    3. Load-time weaving

    It doesn’t do anything at runtime as the classes are compiled directly with aspects.

  2. Spring AOP aims to provide a simple AOP implementation across Spring IoC to solve the most common problems that programmers face. It makes use of only runtime weaving.
    1. JDK dynamic proxy, preferred
    2. CGLIB proxy


6. What is a pointcut, a join point, an advice, an aspect, weaving?

Pointcut
– An expression that selects one or more Join Points
Join Point
– A point in the execution of a program such as a method call or exception thrown
Advice
– Code to be executed at each selected Join Point
Aspect
– A module that encapsulates pointcuts and advice
Weaving
– Technique by which aspects are combined with main code
Introduction
-Spring AOP allows to introduce new interfaces (and a corresponding application) to any object advises.
Target Object
-An object is assisted by one or more respects. Also known as the object advised.
AOP Proxy
-AOP proxy is an object used to perform the contract area. This object is created by the AOP framework. In Spring AOP proxy is part of JDK dynamic proxy or proxy CGLIB.

7. How does Spring solve (implement) a cross cutting concern?

  • Implement your mainline application logic
    • – Focusing on the core problem
  • Write aspects to implement your cross-cutting concerns
    • – Spring provides many aspects out-of-the-box
  • Weave the aspects into your application
    • – Adding the cross-cutting behaviors to the right places

8. Which are the limitations of the two proxy-types?

Spring will create either JDK or CGLib proxies

  1. JDK Proxy
    1. Also called dynamic proxies
    2. API is built into the JDK
    3. Requirements: Java interface(s)
    4. All interfaces proxied
  2. CGLib Proxy
    1. NOT built into JDK
    2. Included in Spring jars
    3. Used when interface not available
    4. Cannot be applied to final classes or methods

9. What visibility must Spring bean methods have to be proxied using Spring AOP?

For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy will be intercepted, and even package-visible methods if necessary. However, common interactions through proxies should always be designed through public signatures.

10. How many advice types does Spring support. Can you name each one? What are they used for?

  • Advice: action taken by an aspect at a join point.

  1. Before advice@Before always proceed to the join point unless an execution is thrown from within the advice code
    • Access control, security
    • Statistics
  2. After returning advice@AfterReturning execution of a join point has completed without throwing any exceptions
    • statistics
    • Data validation
  3. After throwing advice@AfterThrowing invoked after the execution of a join point that resulted in an exception being thrown
    • Error handling
    • Sending alerts when an error has occurred.
    • Attempt error recovery
  4. After (finally) advice@After method will execute after a join point execution, no matter how the execution ended (even exception happens).
    • Releasing resources
  5. Around@Around Around advice can be used for all of the use-cases for AOP.

11. Which two advices can you use if you would like to try and catch exceptions?

@Around. Only around advice allows you to catch exceptions in an advice that occur during execution of a join point.

@Aspect 
 public class Audience {
    
   @Pointcut("execution(** concert.Performance.perform(..))") 
   public void performance() {}
    
   @Around("performance()") 
   public void watchPerformance(ProceedingJoinPoint jp) { 
     try { 
       System.out.println("Taking seats"); 
       jp.proceed(); 
       System.out.println("CLAP CLAP CLAP!!!"); 
     } catch (Throwable e) { 
       System.out.println("Demanding a refund"); 
     }
   }
 }


@AfterThrowing. After throwing advice runs when a matched method execution exits by throwing an exception. The type Throwable is the superclass of all errors and exceptions in the Java language. So, the following advice will catch any of the errors and exceptions thrown by the join points.

@Aspect 
public class CalculatorLoggingAspect {
   
  @AfterThrowing(
    pointcut = "execution(* *.*(..))",
    throwing = "e") 
  public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
    log.error("An exception {} has been thrown in {}()", e, joinPoint.getSignature().getName()); 
  }
}

 

12. What do you have to do to enable the detection of the @Aspect annotation?

3 ways of declaring Spring AOP configuration

  • the ProxyFactoryBean,
  • the aop namespace,
  • and @AspectJ-style annotations.

What does @EnableAspectJAutoProxy do?

 

If shown pointcut expressions, would you understand them?

 

For example, in the course we matched getter methods on Spring Beans, what would be the
correct pointcut expression to match both getter and setter methods?

 

What is the JoinPoint argument used for?

 

What is a ProceedingJoinPoint? When is it used?

 

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *