@Transactional 이 AOP로 동작 한다.
스프링 JPA는 안보이지만 기본적으로 트랜잭션이 적용 되어있다. 명시 하여 override 할 수 있다.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {}
@RestController
public class SampleController {
@LogExecutionTime
public String context() {
return "hello!";
}
}
@Component
@Aspect
public class LogAspect {
Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Around("@annotation(LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object ret = joinPoint.proceed();
stopWatch.stop();
logger.info(stopWatch.prettyPrint());
return ret;
}
}