要求在调用service层的时候输出耗时
-------------------------------------------------------------------------
package cn.tedu.cloud_note.aspect;
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 AuditBean {
//作用:性能审计service层
@Around("within(cn.tedu.cloud_note.service..*)")
public Object audit(ProceedingJoinPoint point){//方法的返回值和参数固定不变
Object obj = null;//obj用于接收point对象调用后的结果,
try {
long t1 = System.currentTimeMillis();
obj = point.proceed();//相当于调用了service层
long t2 = System.currentTimeMillis();
String str = point.getSignature().toString();
System.out.println(str+"耗时: "+(t2 - t1)+" ms");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
}
-------------------------------------------------------------------------
配置文件:spring-aop.xml
<!-- 配置AOP注解扫描 -->
<context:component-scan base-package="cn.tedu.cloud_note.aspect" />
<!-- 开启注解标记 -->
<aop:aspectj-autoproxy />
-------------------------------------------------------------------------