스프링

 

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);
    }

}

 

 

출처:

홍팍

https://www.youtube.com/watch?v=xmiIJvGsvUc&list=PLyebPLlVYXCiYdYaWRKgCqvnCFrLEANXt&index=31&ab_channel=%ED%99%8D%ED%8C%8D

 

 

about author

PHRASE

Level 60  라이트

사람의 성쇠흥망은 마치 아침이 있으면 저녁이 있고 저녁이 있으면 아침이 있는 것같이 교체 변편하는 것. 사람의 생애도 물에 떠도는 부평초 같아 덧없는 것이다. -고시원

댓글 ( 4)

댓글 남기기

작성