記錄一下我在springBoot中用到的切面方法
環(huán)繞切面
目前我用到的環(huán)繞切,有三種
-
第一種方法
/** * 根據(jù)方法簽名進行切面 */ @Around("execution(public * com.example.springboot.web.controller.*.*(..))") public Object fun1(ProceedingJoinPoint point) throws Throwable { // ...(方法執(zhí)行前的邏輯) Object result = point.proceed(); // ...(方法執(zhí)行后的邏輯) return result; // or return null; }
-
第二種方法
/** * 根據(jù)注解進行切面,不獲取注解實例 */ @Around("@annotation(com.example.springboot.MyAnnotation)") public Object fun2(ProceedingJoinPoint point) throws Throwable { // ...(方法執(zhí)行前的邏輯) Object result = point.proceed(); // ...(方法執(zhí)行后的邏輯) return result; }
-
第三種方法
/** * 根據(jù)注解進行切面,同時獲取注解示例 * @param myAnnotation 切點的注解示例儿奶,可以從注解中拿到切點注解的配置 */ @Around("@annotation(myAnnotation)") public Object fun3(ProceedingJoinPoint point, MyAnnotation myAnnotation) throws Throwable { // ...(方法執(zhí)行前的邏輯) Object result = point.proceed(); // ...(方法執(zhí)行后的邏輯) return result; // or return null; }
后置切面
目前只用到一種
/**
* 后置切面,同時獲取方法執(zhí)行結(jié)果
* @param result 就是切點方法的執(zhí)行結(jié)果
*/
@AfterReturning(
value = "execution(public * com.example.springboot.web.controller.*.*(..))",
returning = "result"
)
public Object fun4(JoinPoint point, Object result) throws Throwable {
// ...(方法執(zhí)行后的邏輯)
return result; // or return null;
}