Spring AOP
技術詳解參考
實作測試 - 效能統計(Method執行時間)
annotation方式:
定義一個annotation介面
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}aspect class
@Aspect
@Component
public class LogExecutionTimeAop {
Logger log = LoggerFactory.getLogger("aopTest");
@Pointcut("@annotation(com.ecommerce.mini.anno.LogExecutionTime)")
public void logExecution() {
}
@Around("logExecution()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.debug("---> around");
long start = System.currentTimeMillis();
Object obj = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
log.debug("---> around " + joinPoint.getSignature() + " executed in " + executionTime + "ms");
return obj;
}
@After("logExecution()")
public void after(JoinPoint joinPoint) {
log.debug("---> after");
}
@AfterReturning(value = "logExecution()", returning = "returnObject")
public void afterReturning(JoinPoint joinPoint, Object returnObject) {
log.debug("---> AfterReturning");
}
@Before("logExecution()")
public void before(JoinPoint joinPoint) {
log.debug("---> before");
}
}Controller
output
execution方式:
aspect class 增加
output
實作測試 - 日誌紀錄
aspect class
Controller
output
結語

Last updated