From a9eb4d3cf092c403309fa88af62813d17db85dbb Mon Sep 17 00:00:00 2001 From: JuYeong17 Date: Mon, 18 Dec 2023 00:38:06 +0900 Subject: [PATCH] AOP --- .../com/example/SpringVol1/TimeTrace.java | 11 +++++++ .../example/SpringVol1/TimeTraceAspect.java | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/main/java/com/example/SpringVol1/TimeTrace.java create mode 100644 src/main/java/com/example/SpringVol1/TimeTraceAspect.java diff --git a/src/main/java/com/example/SpringVol1/TimeTrace.java b/src/main/java/com/example/SpringVol1/TimeTrace.java new file mode 100644 index 0000000..30f8f58 --- /dev/null +++ b/src/main/java/com/example/SpringVol1/TimeTrace.java @@ -0,0 +1,11 @@ +package com.example.SpringVol1; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface TimeTrace { +} diff --git a/src/main/java/com/example/SpringVol1/TimeTraceAspect.java b/src/main/java/com/example/SpringVol1/TimeTraceAspect.java new file mode 100644 index 0000000..6e62817 --- /dev/null +++ b/src/main/java/com/example/SpringVol1/TimeTraceAspect.java @@ -0,0 +1,31 @@ +package com.example.SpringVol1; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; +import org.springframework.util.StopWatch; + +@Component +@Aspect +public class TimeTraceAspect { + + // @Pointcut("execution(* com.example.demo.repository..*(..))") + @Pointcut("@annotation(com.example.demo.config.aop.TimeTrace)") + private void timeTracePointcut() { + } + + @Around("timeTracePointcut()") + public Object traceTime(ProceedingJoinPoint joinPoint) throws Throwable { + StopWatch stopWatch = new StopWatch(); + + try { + stopWatch.start(); + return joinPoint.proceed(); // 실제 타겟 호출 + } finally { + stopWatch.stop(); + + } + } +} \ No newline at end of file