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.
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?
A 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.
Other examples of cross-cutting concerns
Not a cross-cutting concerns
Two popular AOP libraries
It doesn’t do anything at runtime as the classes are compiled directly with aspects.
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?
8. Which are the limitations of the two proxy-types?
Spring will create either JDK or CGLib proxies
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.
@Before
always proceed to the join point unless an execution is thrown from within the advice code
@AfterReturning
execution of a join point has completed without throwing any exceptions
@AfterThrowing
invoked after the execution of a join point that resulted in an exception being thrown
@After
method will execute after a join point execution, no matter how the execution ended (even exception happens).
@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
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?