我想在调用某些服务方法时记录花在日志文件上的时间,现在我按AOP
实现它,例如
@Around("execution(* sample..TaskService.*(..))")
public Object aroundStat(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long end = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
log.info("{} taken time: {} ms",methodName,(end-start));
return proceed;
}
但我想知道如何使用注释来实现它,就像@Trasactional
一样,例如。
@Service
@TimeLogging
public class TaskService {
@TimeLogging
List<TaskStatDTO> taskStatusStat(String name){
//...
}
List<TaskStatDTO> finishedTaskStat(String name){
//...
}
//...
}
我可以实现一些类并重写一些方法吗?
答案 0 :(得分:0)
long start = System.currentTimeMillis(); long end = System.currentTimeMillis(); (end-start)
更改为
StopWatch watch = new StopWatch();
watch.start();
..... execution
watch.stop();
watch.getTotalTimeMillis()
而不是
@Around(&#34;执行(* sample..TaskService。*(..))&#34;)
使用
@Around(&#34;执行(* sample..TaskService。(..)&amp;&amp; @annotation( sample ... TimeLogging)&#34;)
更好:请参阅Combining pointcut expressions和Sharing common pointcut definitions
@Pointcut("execution(@annotation(* sample...TimeLogging)")
private void withTimeExecutionLayer() {}
@Pointcut("execution(public *sample..service.* *(..))")
privatr void inServiceLayer() {}
@Pointcut("inServiceLayer() && withTimeExecutionLayer()")
private void auditTimeExecutionServiceLayer() {}