spring framework Simple study memo (2): AOP
spring framework Simple study memo (2): AOP
Essence
--Separate scattered code from essential processing
Way of thinking
--Implementing scattered processing across
Typical application scene
--Log output
--Security check
--Transaction
--Cashew
--Monitoring
--Exception handling
Source example
@Aspect
@Component
public class MethodStartLoggingAspect {
@Before("execution(* *..*ServiceImpl.*(..))")
public void startLog(JoinPoint jp) {
System.out.println("Method start:" + jp.getSignature());
}
}
Advice implementation method
- @Before
--join point Previous implementation
- @AfterReturning
--Implemented after join point ends normally
--Not implemented when throwing an exception
- @AfterThrowing
--Implemented when an exception is thrown
--Do not execute after join point ends normally
- @After
--Implemented after the end regardless of the exception throw
- @Around
--Implemented before and after join point
point cut formula
--How to select a join point
--Example: "execution (* * .. * ServiceImpl. * (..))"
--Specified method
--Method specification
--Class type specification
--Other specifications such as name
AOP regular annotation
--Transaction
- @Transaction
--Commit when method ends normally
--Rollback on abnormal method termination
--Authentication
- @PreAuthorize
--Cashew
- @Cacheable
--Asynchronous processing
- @Async
--Retry processing
- @Retryable