From a1c3594106c65f0b2694673e1cc08136857a655b Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Fri, 23 Feb 2024 14:43:29 +0900 Subject: [PATCH] =?UTF-8?q?AspectServiceBean=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/AspectServiceBean.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/main/java/cokr/xit/foundation/component/AspectServiceBean.java diff --git a/src/main/java/cokr/xit/foundation/component/AspectServiceBean.java b/src/main/java/cokr/xit/foundation/component/AspectServiceBean.java new file mode 100644 index 0000000..43b82ab --- /dev/null +++ b/src/main/java/cokr/xit/foundation/component/AspectServiceBean.java @@ -0,0 +1,87 @@ +package cokr.xit.foundation.component; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +/**Aspect 구현을 지원하는 ServiceBean. + *

다음 포인트컷에 해당하는 서비스와 컨트롤러의 메소드 앞뒤에 실행할 기능의 구현을 지원한다. + *

+ * @author mjkhan + */ +@Aspect +public abstract class AspectServiceBean extends AbstractServiceBean { + private static final String + SVC_POINT = "execution(* cokr.xit..service.bean.*ServiceBean.*(..))", + CNTR_POINT = "execution(* cokr.xit..*Controller.*(..))"; + + /**컨트롤러의 메소드 앞에 실행된다. + * @param joinPoint JoinPoint + */ + @Before(CNTR_POINT) + public void beforeController(JoinPoint joinPoint) {} + + /**서비스의 메소드 앞에 실행된다. + * @param joinPoint JoinPoint + */ + @Before(SVC_POINT) + public void beforeService(JoinPoint joinPoint) {} + + /**서비스의 메소드 정상 종료 후에 실행된다. + * @param joinPoint JoinPoint + * @param returned 메소드가 반환하는 값 + */ + @AfterReturning(value = SVC_POINT, returning = "returned") + public void afterServiceReturn(JoinPoint joinPoint, Object returned) {} + + /**서비스의 메소드 실행 중 오류가 발생했을 때 실행된다. + * @param joinPoint JoinPoint + * @param thrown 메소드 실행 중 발생한 오류 + */ + @AfterThrowing(value = SVC_POINT, throwing = "thrown") + public void afterServiceThrow(JoinPoint joinPoint, Throwable thrown) {} + + /**서비스의 메소드 정상/오류 종료 후에 실행된다. + * @param joinPoint JoinPoint + */ + @After(SVC_POINT) + public void afterService(JoinPoint joinPoint) {} + + /**컨트롤러의 메소드 정상 종료 후에 실행된다. + * @param joinPoint JoinPoint + * @param returned 메소드가 반환하는 값 + */ + @AfterReturning(value = CNTR_POINT, returning = "returned") + public void afterControllerReturn(JoinPoint joinPoint, Object returned) {} + + /**컨트롤러의 메소드 실행 중 오류가 발생했을 때 실행된다. + * @param joinPoint JoinPoint + * @param thrown 메소드 실행 중 발생한 오류 + */ + @AfterThrowing(value = CNTR_POINT, throwing = "thrown") + public void afterControllerThrow(JoinPoint joinPoint, Throwable thrown) {} + + /**컨트롤러의 메소드 정상/오류 종료 후에 실행된다. + * @param joinPoint JoinPoint + */ + @After(CNTR_POINT) + public void afterController(JoinPoint joinPoint) {} + + /**로그 메시지를 출력한다. + * @param str + * @param joinPoint JoinPoint + */ + protected void log(String str, JoinPoint joinPoint) { + log().debug( + "{}({}): {}", + str, + Thread.currentThread().getId(), + joinPoint.getTarget().getClass().getName() + "#" + joinPoint.getSignature().getName() + ); + } +} \ No newline at end of file