spring 切面编程

spring支持的切面通知类型:

  • Before——前置通知,在调用目标方法之前执行通知定义的任务;
  • After——后置通知,在目标方法执行结束后,无论执行结果如何都执行通知定义的任务;
  • After-returning——后置通知,在目标方法执行结束后,如果执行成功,则执行通知定义的任务;
  • After-throwing——异常通知,如果目标方法执行过程中抛出异常,则执行通知定义的任务;
  • Around——环绕通知,在目标方法执行前和执行后,都需要执行通知定义的任务。

spring aop就是一个同心圆,要执行的方法为圆心,最外层的order最小。从最外层按照AOP1、AOP2的顺序依次执行doAround方法,doBefore方法。然后执行method方法,最后按照AOP2、AOP1的顺序依次执行doAfter、doAfterReturn方法。也就是说对多个AOP来说,先before的,一定后after。
如果我们要在同一个方法事务提交后执行自己的AOP,那么把事务的AOP order设置为2,自己的AOP order设置为1,然后在doAfterReturn里边处理自己的业务逻辑。


execution表达式

表达式结构解释如下:
这里写图片描述

1.通过方法修饰符定义切点 
匹配所有的public修饰符的方法:
execution(public * *(..))

2.通过方法名定义切点
 匹配所有”set”开头的方法:
execution(* set*(..))

3.通过类定义切点
匹配AccountService 接口的所有方法:
execution(* com.xyz.service.AccountService.*(..))

4.通过包定义切点
匹配service包中的所有方法:
execution(* com.xyz.service..(..))

 匹配service包及其子包中的所有方法:
execution(* com.xyz.service…(..))


/**
 * Created by jiyang on 10:30 2017/12/22
 */
@Component
@Aspect
@Slf4j
@Profile("dev")
@Order(2)
public class ForTestAspect1 {

    @Before("execution(* com.myspringboot.test.controller..*(..))")
    public void serviceExec(JoinPoint pjp) {
//        Signature signature = pjp.getSignature();
//        Object[] args = pjp.getArgs();
//        Object target = pjp.getTarget();
//        Object thisObject = pjp.getThis();

        //ProceedingJoinPoint 对象是JoinPoint的子接口,该对象在around增加了
        // 执行目标方法
        //pjp.proceed();
        // 传入新的参数执行目标方法
        //pjp.proceed(Object[] var1)

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();

        Annotation annotation = method.getDeclaredAnnotation(RequestMapping.class);
        if (annotation == null) {
            return;
        }

        // 获取所有注解
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            Annotation[] at = annotations[i];
            for (Annotation a : at) {
                // 判断注解
                if (a.annotationType() == RequestParam.class) {
                    RequestParam a1 = (RequestParam) a;
                    if ("token".equals(a1.value())) {
                        Object[] args = pjp.getArgs();
                        System.out.println((String) args[i]);
                    }
                }
                if (a.annotationType().equals(PathVariable.class)) {
                    PathVariable a1 = (PathVariable) a;
                    if ("token".equals(a1.value())) {
                        Object[] args = pjp.getArgs();
                        System.out.println((String) args[i]);
                    }
                }
            }

        }
    }
}
/**
 * Created by jiyang on 12:39 2017/12/22
 */
@Component
@Aspect
@Profile("dev")
@Slf4j
@Order(1)
public class ForTestAspect2 {

    @Around("execution(* com.myspringboot.test.controller..*(..))")
    public Object serviceExec(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        Type[] types = method.getGenericParameterTypes();
        String info = method.getDeclaringClass() + "." + method.getName() + "()";

        long t1 = System.nanoTime();
        log.debug("[" + t1 + "] service method " + method + " args:" + Arrays.toString(types));
        try {
            return pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw e;
        } finally {
            long t2 = (System.nanoTime() - t1) / 1000000;
            log.info("[" + t1 + "] " + info + " takes " + t2 + " ms ");
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值