Παρασκευή 5 Νοεμβρίου 2021

Spring Certification: Aspect-Oriented Programming (AOP)

What is the concept of AOP? Which problem does it solve? What is a cross cutting concern?

  • AOP is a programmatic technique to apply separation of concerns regarding cross-cutting concerns, like transaction management, security, logging and more. It allows to decouple these concerns from tangling with the business code. Thus, reduces duplicating same code. It uses "advices" applied on "pointcuts" upon calling.
  • Cross-cutting corcerns are basic functionalities that complement the business code, like the mentioned above.


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

  • Pointcut: predicate that matches join points with advice
  • Join Point: represents the effective execution of a method where the aspect applies
  • Advice: the action taken by Aspect at a join point
  • Aspect: modularization of a concern that cuts across multiple classes
  • Weaving: program transformation that applies the aspect to the target object, in order to create the advised object (target object). Happens in runtime for Spring AOP, and in compile-time and runtime for AspectJ.



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

  • Spring makes use of  proxy objects that wrap the target bean and intercept any method invocations that are defined by the pointcuts of an advice. These proxies are created at runtime, and can be JDK Dynamic proxies (Spring default) or CGLIB proxies


Which are the limitations of the two proxy-types?
  • JDK Dynamic Proxy
    • Target class must implement an interface
    • Only public methods that reside in interface can be proxied
    • If target method is calling another method, proxy won't work for this second method
  • CGLIB proxy
    • Target class and methods must not be final
    • Protected and public visibilities are supported
    • No self-invocation as for JDK Dynamic proxy
    • Needs and extra library spring-core, while JDK Dynamic proxy is built-in


How many advice types does Spring support? Can you name each one?

  • Before advice
    Performs some action, and then proceeds to execution of JoinPoint
    If any exception occurs though, it does not proceed.



  • After (finally) advice
    Always executes after target method has run, regardless of exception or success.





  • After Returning advice
    Executes after successful completion of JoinPoint's target method
    Provides access to returned object with "returning" attribute



  • After Throwing advice
    Executes only if target method contains an exception
    Provides access to returned object with "throwing" attribute


  • Around advice
    The most useful advice, it can manipulate the target method's params, catch exceptions from it and advice itself, and handle normal execution result.
    It can catch any exception of target method execution inside it immediately



What do you have to do to enable the detection of the @Aspect annotation? What does @EnableAspectJAutoProxy do?
  • We would need to enable AspectJ support, thus add spring-aspects dependency in pom
  • Annotate a @Configuration file with @EnableAspectJAutoProxy
  • Finally, in our @Component that we'll apply advices and pointcuts, annotate it with @Aspect
    If we have @Bean for this, annotate Bean POJO class with @Aspect
  • @EnableAspectJAutoProxy annotation activates the support for components marked with AspectJ’s @Aspect annotation 


If shown pointcut expressions, would you understand them?
  • execution(public * com.code.in.packets.SomeBean.*(int, ..))
    Targets all methods of SomeBean of com.code.in.packets package, that return anything, are public and take one int and any other parameters
  • within(com.code.in.*)    
    Targets all methods of all classes in com.code.in package and all subpackages
  • within(SomeInterface+)
    Matches for all the methods of classes that implement the 
    SomeInterface
  • within(com.code.in.packets.SomeService+)
    Matches for SomeService class and for all of its subclasses


What is the JoinPoint argument used for?
  • Upon advice invocation, JoinPoint parameter holds reference to an object that holds static information about the join point. From a JoinPoint we can access:
  1. target object - getTarget()
  2. method signature - getSignature()
  3. method arguments - getArgs() methods.


What is a ProceedingJoinPoint? Which advice type is it used with?
  • ProceedingJoinPoint is a type JoinPoint, used on @Around advice.
  • It is the first parameter of the advice
  • It has a proceed() method used to call the join point, which also could alter the parameters of the target method

-----------------------------------------------------------------------------------------------------


Questions from EDU-1202 exam (2021)


What advice is used to stop the propagation of an exception up the call stack?
  • Around advice can catch any exception from the target method execution, and throw an other, or do something else. Because Around wraps the executed method, it has complete control over the flow.

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?