[AOP] Spring AOP 정리
Last updated
Last updated
dependencies {
// ...
implementation 'org.springframework:spring-aspects'
// ...
}package com.example.funch.order;
// import ... 생략
@Service
public class OrderService {
public String purchase(String userId) {
// 복잡한 비즈니스 로직
return puchaseId;
}
}package com.example.funch.order.aop;
// import ... 생략
@Component
@Aspect // 부가 관심사 등록
public class OrderActionAspect {
// Around: Advice 옵션
// execution: Point Cut 옵션
@Around("execution(* com.example.funch.order.OrderService.purchase(..))")
public Object printLog(ProceedingJoinPoint jointPoint) throws Throwable {
// 사용자 행위에 대한 로그 출력 또는 저장 로직
return joinPoint.proceed();
}
}package com.example.funch.order.aop;
// import ... 생략
@Component
@Aspect // 부가 관심사 등록
public class OrderNotificationAspect {
// AfterReturning: Advice 옵션
// execution: Point Cut 옵션
@AfterReturning(
pointcut = "execution(* com.example.funch.order.OrderService.purchase(...))",
returning = "result"
)
public void send(JoinPoint joinPoint, String result) {
// 사용자 구매 알림 발송 로직
}
}