Spring
[프로젝트] Custom Annotation으로 알림 기능 Spring AOP 적용
맛도리얌
2023. 6. 29. 15:20
yata project에서는 알림 기능을 만들며,
Spring AOP를 통해 기능분리를 하였고,
유지보수성, 확정성을 위해 Spring Annotation을 활용하여 적용하였다.
Custon Annotation이란? (현재 비공개 포스팅)
1.Custom Annotation 정의
Annontation을 정의할 때는 @interface를 붙여 작성한다.
📌적용된 메타에너테이션
@Target -> NeedNotify를 메서드 단위에 붙여 사용
@Retention -> NeedNotify의 기능이 런타임시까지 유지
2. NotifyAspect 정의
부가 기능 (Advice) + 적용할 위치 (Pointcut)
@Aspect
@Slf4j
@Component
@EnableAsync
public class NotifyAspect {
// yataRequestService createRequest method pointcut
private final NotifyService notifyService;
public NotifyAspect(NotifyService notifyService) {
this.notifyService = notifyService;
}
@Pointcut("@annotation(com.yata.backend.domain.notify.annotation.NeedNotify)")
public void annotationPointcut() {
}
@Async
@AfterReturning(pointcut = "annotationPointcut()", returning = "result")
public void checkValue(JoinPoint joinPoint, Object result) throws Throwable {
NotifyInfo notifyProxy = (NotifyInfo) result;
notifyService.send(
notifyProxy.getReceiver(),
notifyProxy.getNotificationType(),
NotifyMessage.YATA_NEW_REQUEST.getMessage(),
"/api/v1/yata/" + (notifyProxy.getGoUrlId())
);
log.info("result = {}", result);
}
}
3. 사용할 메서드에 NeedNotify annotation붙이기
알림 기능이 필요한 메서드에 해당 Annotation 붙여줌
야타 신청시 알림기능이 필요했으므로 붙여주었다.