Skip to content

Instantly share code, notes, and snippets.

@yusufcakal
Last active July 7, 2019 19:36
Show Gist options
  • Save yusufcakal/3fc39784ba0bd89ade2940888497ae3f to your computer and use it in GitHub Desktop.
Save yusufcakal/3fc39784ba0bd89ade2940888497ae3f to your computer and use it in GitHub Desktop.
This custom annotation shows execution time that used method via aspect oriented programming in spring.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
@Aspect
@Component
public class LogExecutionTimeAspect {
@Around("@annotation(LogExecutionTime)")
public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
joinPoint.proceed();
String className = joinPoint.getTarget().getClass().getSimpleName();
String methodName = joinPoint.getSignature().getName();
Logger LOGGER = LoggerFactory.getLogger("Execution time of " + className + "." + methodName);
LOGGER.info(System.currentTimeMillis() - startTime + " ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment