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

编程技术  /  houtizong 发布于 3年前   137
  1. 实现功能:
  • 利用spring aop annotation实现切片操作,并改变方法参数的值
  • 判断方法或方法的参数上是否有特定的annotation,如果有,则按照其它逻辑进行处理
  1. aop切片操作
@Component@Aspectpublic 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;    }
     

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客