Spring AOP Flashcards
What are cross cutting concerns?
Functionalities that are orthogonal to the business requirement and cut across multiple points of application are called cross cutting concerns. Some examples are like Authentication, Authorization, Logging, Metrics, etc.
Give example of common cross cutting concerns
Logging, Security, Transaction management
What does AOP provide us?
Ability to decouple these cross cutting concerns from main business logic and make the code resuable.
Which are various AOP jargons?
1) Aspect
2) Advice
3) Pointcut
4) JoinPoint
What is Aspect?
Cross cutting concerns can be modularized into special classes called aspects.
What is Advice?
The piece of code that is invoked during program execution. Advice defines both when and what of aspect.
What is Joinpoint?
This represents a point in your application where you can plug in AOP aspect. In spring a join point is always a method.
What is pointcut?
Pointcuts define the when of aspect. It is an expression which defines filter. This is a set of one or more joinpoints where an advice should be executed.
What is weaving?
Weaving is the process of linking the aspets with other application types or objects to create an adviced object. There are two types of weaving.
1) Compile time weaving
2) Runtime weaving.
Which expression types are supported when defining pointcut?
1) execution
2) args
3) within
4) this
5) target
Define execution expression in pointcut
For matching method execution join points, this is primary pointcut designator what you will use
Define args expression in pointcut
Limits matching to joinpoints where the arguments are instances of the given types.
Define within expression in pointcut
Limits matching to join points within certain types
Define this expression in pointcut
Limits matching to joinpoints where the bean reference is an instance of the given type.
Define target expression in joinpoint
Limits matching to joinpoints where the target object is an instance of the given type.
Pointcut expression for any method named ‘setData’
@PointCut(“execution(* setData(..))”)
Pointcut expression for execution of any method defined by the AccountService class
@Pointcut(“execution(* co.edureka.spring.service.AccountService.*(..)”)
Pointcut expression for execution of any method within the controllers package or a sub-package
within(co.edureka.controllers..*)
Pointcut expression for execution of any method which takes a single parameter and where the argument passed at runtime is Date
args(java.util.Date)
Pointcut expression for execution of any method where the target object implements the TransactionService interface
target(co.edureka.service.TransactionService)
How many ways can you do AOP
Spring XML based or AspectJ annotation driven aspects
Advice types supported in spring
Before, After, AfterReturning, AfterThrowing, Around
Which interface is used to implement Before advice
MethodBeforeAdvice interface and it has before method.
Which interface is used to implement After returning advice
AfterReturningAdvice interface is used and it has method afterReturning