注入拦截器
@Aspect
@Slf4j
@Component
public class ServiceFilter {
@Pointcut("execution(* com.demo.Service.*(..))")
public void pointcutService() {}
/**
* 打印方法执行时间
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("pointcutService()")
public Object calCost(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long cost = System.currentTimeMillis() - startTime;
log.info("Service." + joinPoint.getSignature().getName() + " method cost: " + cost + "ms.");
return result;
}
/**
* 在方法执行前
* @param point 切点
*/
@Before("pointcutService()")
public static void before(JoinPoint point){
log.info("Before of Service." + point.getSignature().getName() );
}
/**
* 在方法执行后
* @param point 切点
*/
@After("pointcutService()")
public void after(JoinPoint point){
log.info("After of Service." + point.getSignature().getName() );
}
/**
* 在方法返回后
* @param point 切点
*/
@AfterReturning("pointcutService()")
public void afterReturning(JoinPoint point){
log.info("AfterReturning of Service." + point.getSignature().getName() );
}
/**
* 在方法抛出异常后
* @param point 切点
*/
@AfterThrowing("pointcutService()")
public void afterThrowing(JoinPoint point){
log.info("AfterThrowing of Service." + point.getSignature().getName() );
}
}
execution的简单说明
execution(返回值 包路径.类.方法名(…))
参考文章
我只是对文章的内容做了实用的总结,想更多的了解AOP请学习原文。