MyRunningTime
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRunningTime {
}
MyRunningTime 어노테이션 AOP 적용
PerformanceAspect
import lombok.extern.slf4j.Slf4j;
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;
@Aspect
@Component
@Slf4j
public class PerformanceAspect {
@Pointcut("@annotation(프로젝트패키지경로.MyRunningTime)")
private void enableRunningTime(){}
@Pointcut("execution(* 프로젝트패키지경로..*.*(..))")
private void cut(){
}
@Around("cut() && enableRunningTime() ")
public void loggingRunningTime(ProceedingJoinPoint joinPoint) throws Throwable{
StopWatch stopWatch=new StopWatch();
stopWatch.start();
Object returnigObj=joinPoint.proceed();
stopWatch.stop();
String methodName=joinPoint.getSignature().getName();
log.info("{} 의 총 수행 시간 => {} , sec ", methodName, stopWatch.getTotalTimeSeconds());
}
}
어노테이션 사용예
@RestController
@RequestMapping("/api/articles/comments/")
public class CommentApiController {
@Autowired
private CommentServie commentServie;
@MyRunningTime
@GetMapping("{articleId}")
public ResponseEntity comments(@PathVariable Long articleId){
List dtos=commentServie.comments(articleId);
return ResponseEntity.status(HttpStatus.OK).body(dtos);
}
}
출처:

















댓글 ( 4)
댓글 남기기