1.在pom.xml中导入坐标
<!--AOP面向切面编程-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
</dependency>
2.编写Aspect类
package com.example.store.util;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TimeAOP {
//ProceedingJoinPoint表示连接点
@Around("execution(* com.example.store.service.impl.*.*(..))")
public Object arround(ProceedingJoinPoint point) throws Throwable{
//记录开始执行的时间
Long start =System.currentTimeMillis();
Object result =point.proceed();//调用目标方法
//记录结束时间
Long end =System.currentTimeMillis();
System.out.println("耗时 :" +(end-start));
return result;
}
}