22-AOP Flashcards
What are cross-cutting concerns? Give some examples
Generic functionality that is needed in many places in your application Examples: 1. Logging and tracing 2. Security 3. Custom Business Rules
What does Spring-AOP enable?
Modulization of cross-cutting concerns i.e. The code for a cross-cutting concern is in a single place, in a module - in Java, a class represents a module
What’re the problems if failing to modularize cross-cutting concerns?
- Code tangling i.e. coupling of concerns
2. Code scattering i.e. the same concern spread across modules
What is aspect weaving?
Technique by which aspects are combined with main code
What library does Spring AOP use?
AspectJ
What is a join point?
A point in the execution of a program such as a method call or exception thrown
What is a pointcut?
An expression that selects one or more Join Points
What is an Advice?
Code to be executed at each selected JoinPoint
What is an Aspect?
A module that encapsulates pointcuts and advice
What is AOP proxy?
An “enhanced” class that stands in place of your original. With extra behavior (Aspect) added (woven) into it
Write an aspect component to advise all functions start with set and return void
@Aspect @Component public class Tracker { @Before("execution(void set*(*))") public void method() {} }
List out all @EnableXXX annotations
@EnableAspectJAutoProxy
- @EntityScan w/SpringBootApplication
- @EnableJpaRepository(basePackage=”…”) with @Configuration
- @EnableWebSecurity with @Configuration
- @EnableGlobalMethodSecurity with
- @AutoConfigureMockMvc with @SpringBootTest
- @AutoConfigureTestDatabase
- @EnableHypermediaSupport(type=HypermediaType.HAL) with @Configuration
- @EnableWebMvc with @Configuration
- @EnableAutoConfiguration
- @EnableConfigurationProperties
- @EnableTransactionManager
What is @EnableAspectJAutoProxy equivalent to?
XML: aop:aspectj-autoproxy
What aop:aspectj-autoproxy supports but @EnableAspectJAutoProxy doesn’t?
it has aop:include to sepecify ids if beans to use in aspect –> doesn’t need to scan all aspect components. But Spring AOP doesn’t have this
How to enable AOP in Spring app?
no need for Spring Boot app. Need to add @Configuration @EnableAspectJAutoProxy for non-Spring boot app
When is the point-cut expression used?
- “weaving” aspect and target
2. on every invocation, to decide if the advice should actually apply
List out pointcut designators
within: package
execution
How to chain pointcuts together?
Using: && or || or !
What is the method pattern in execution(method-pattern)?
[Modifiers] ReturnType [ClassType]
MethodName (Arguments) [throws ExceptionType]
What are mandatory in pointcut method pattern?
[Modifiers] ReturnType [ClassType]
MethodName (Arguments) [throws ExceptionType]
Can the pointcut work with the private/protected method in class?
yes if using CGLIB proxies
Using pointcut, how to match method with 0, 1, 2 and any arguments
0: set()
1: set()
2: set(*, )
any: set(..)
The powerful match-all pointcut expression
- *(..)
Pointcut: match method starts with send, having an argument of type rewards.Dining and return void
execution(void send*(rewards.Dining))