spring aop中实现变更方法参数以及annotation

  1. 实现功能:
  • 利用spring aop annotation实现切片操作,并改变方法参数的值
  • 判断方法或方法的参数上是否有特定的annotation,如果有,则按照其它逻辑进行处理
  1. aop切片操作
@Component
@Aspect
public class AopAnnotation {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(public * com.*.*.*(..))")
    public void pointCut(){

    }

    @Before(value = "pointCut()")
    public void before(){
        logger.info("annotation,   before################################");
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint){
        logger.info("annotation,   around################################");
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.start();
        try {
            joinPoint.proceed();
        } catch (Throwable e) {

        }
        stopwatch.stop();
        logger.info("annotation, ###################cost time: " + joinPoint.getThis().getClass().getName() +
                "\t" + stopwatch.elapsedMillis() + "(ms)");
    }

    @AfterThrowing(pointcut = "pointCut()")
    public void aa(){
          logger.info("error");
    }

    
    public Verifier verifier;
}

 

  1. 声明annotation
  • @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface Param {
      String value();
    }
     
  1. 在joinpoint中进行处理,判断参数是否有annotation
  • @Around("sqlEscapePoint()")
        public Object sqlEscapeAdvice(ProceedingJoinPoint joinPoint) {
            logger.info("aroundSqlEscape,   around################################");
            Object[] objects = joinPoint.getArgs();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Annotation[][] annotations = method.getParameterAnnotations();
            for (int i = 0; i < objects.length; i++) {
                Object tmpObject = objects[i];
                boolean notEscape = false;
                if (annotations.length > 0) {
                    for (Annotation annotation : annotations[i]) {
                        if (annotation.annotationType() == NotEscapeSql.class) {
                            notEscape = true;
                        }
                    }
                }
                if (notEscape) {
                    continue;
                }
                objects[i] = EscapeUtil.escape(tmpObject, false, false, true);
            }
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.start();
            Object result = null;
            try {
                result = joinPoint.proceed(objects);
            } catch (Throwable e) {
                logger.error("sql escape", e);
            }
            stopwatch.stop();
            logger.info("mapper cost time: " + joinPoint.getThis().getClass().getName() + ","
                    + method.getName() + "\t" + stopwatch.elapsedMillis() + "(ms)");
            return result;
        }
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值