Week 7 Aspect Oriented Programming Flashcards
What are Cross-cutting concerns?
Cross-cutting-concerns are functionalities used throughout an application (it “cuts” across different layers, classes, methods, etc.)
Examples: logging, security, validation
What is an Aspect?
An Aspect is a class that contains different Advice (structured as methods)
Where is the @Aspect annotation from?
@Aspect is from the AspectJ library, which is a full AOP framework, but Spring AOP can also utilize AspectJ annotations
Note: We can annotate our class with @Aspect to address a particular concern
What is AspectJ? What is the advantage and disadvantage?
AspectJ is a fully fledged ORIGINAL AOP framework that provided the ability to write AOP code in an annotation based format.
Advantage: More functionality than Spring AOP
Disadvantage: Harder to Use
What are some features of Spring AOP?
To make use of annotations, Spring AOP understands AspectJ annotations
Less functionality than AspectJ
Easier to use
Uses “runtime-weaving”, which is a process where code is “wrapped” during runtime
What is a Join Point?
A join point is the location that is “intercepted” by the advice code
A JoinPoint is represented by method execution So when some method is invoked, the advice will intercept it
What is a PointCut?
A pointcut is a predicate (similar to regular expressions) that indicates WHAT we actually want to intercept
Used to signify the actual JoinPoint
What is Advice?
Advice is the actual code that will run during interception of a join point (specified by a pointcut expression)
What are some types of Advice?
@Before: Advice that will execute before the JoinPoint is executed
@After: Advice that will execute after the JoinPoint is executed (whether it throws an exception or returns successfully)
@AfterReturning: Advice that will execute after the JoinPoint is executed only when there is a successful return with no exception thrown
@AfterThrowing: Advice that will execute after the JoinPoint is executed only when a particular exception is thrown
@Around: Allows for the intercepting of a method both before and after
Can do things like stop the execution of the JoinPoint, or even stop an exception from propagating
Ex. a method mapped to an endpoint
-Using @Around advice, we can actually prevent the endpoint from executing if a condition is not met (ex. current user is not an Admin)
@Around is the MOST POWERFUL type of advice
What can JoinPoint interface do?
gives us access to the target object
allows us to gain information such as the method signature, method parameters, etc.
What can ProceedingJoinPoint interface do?
Used to handle @Around advice
gives access to the .proceed() method, which will actually execute the JoinPoint
What are some Types of PointCut Expressions?
@annotation: This one can be used for any method annotated with the specified annotation
execution: Most common, specifies which methods to have as our JoinPoints
modifiers-pattern: public private protected default
return-type: void, String, int, boolean, com.revature.gradifysb.model.Assignment
declaring-type-pattern: class name that the method belongs to
Parameter pattern wildcards:
- (): match a method with no arguments
- (*): matches a method with 1 argument of any type
- (..): matches a method with 0 or more arguments of any type
Combine pointcuts
- && (AND)
- || (OR)
- ! (NOT)
Define Aspect.
Aspect: the class that contains advice methods
Define Advice:
Advice: a method that will execute for a particular Join Point
Define JoinPoint:
Join Point: the actual method being intercepted