使用方式1,省略如下注解
//@Around("execution(public * com.bd.xxxx.xx.xxx..*.*(..))")
用法
1.列:自定义注解GmallCache
//自定义注解
@Target(value=ElementType.METHOD)//限制运行在方法上
@Retention(value=RetentionPolicy.RUNTIME)//运行时有效
public@interfaceGmallCache {
}
2.为需要代理的目标方法上加入自定义的注解 @GmallCache
@GmallCache
@Override
publicSkuInfofindSkuById() {}
3.使用,可以省@Around("@annotation(com.atguigu.gmall.item.anno.GmallCache)")
// @Around("@annotation(xxx.xxx.xx.xx.GmallCache)")
@Around("@annotation(gmallCache)")
publicObjectaround(ProceedingJoinPointproceedingJoinPoint , GmallCachegmallCache) throwsThrowable { // 环绕通知
log.info("AopAspect2.....around..前..方法执行了");
Objectproceed=null ;
try {
proceed=proceedingJoinPoint.proceed();
log.info("AopAspect2.....around..后..方法执行了");
}catch (Exceptione) {
e.printStackTrace(); // 异常后通知
} finally {
// 最终通知
}
System.out.println(proceed);
returnproceed ;
}
}
多切面的执行顺序:
按照@Order指定的注解顺序排序;数字越小,优先级越高,优先级越高越优先执行
如果@Order的值是相同的,按照切面类名的字母顺序进行排序
如果@Order使用的都是默认值,事务切面的优先级最高
事务:异常类型错误:事务默认回滚的是:RuntimeException,如果异常被try catch 后未抛出运行时异常 事务就会失效如果你想触发其他异常的回滚,需要在注解上配置一下,如:@Transactional(rollbackFor = Exception.class)