在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数、注解做一个样例。
@target(elementtype.method)@retention(retentionpolicy.runtime)@documentedpublic @interface anndemo { string value(); boolean isaop() default true;}
@restcontroller@requestmapping("/order")public class ordercontroller { @autowired pri烈火英雄电影vate orderrvice orderrvice; @requestmapping("/all") @anndemo(value = "all",isaop = fal) 一拍两散歌词public list<tborder> findall() { list<tborder> list = orderrvice.getorderlist(); return list; } @requestmapping("/page") @anndemo(value = "page") public list<tborder> findpage(@requestparam("urname") string urname) { list<tborder> listpage = orderrvice.getorderslistpage(); return listpage; }}
在切面中获取切点注解,方法,参数的获取
@aspect@componentpublic class aspectdemo { @pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))") public void excetionmethod() {} @pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(anndemo)") public void excetionnote() { } @before("excetionmethod()") public void testbefore(joinpoint joinpoint) { 幻听 许嵩system.out.println("----------------------------前置通知---"); object[] args = joinpoint.getargs(); for (object arg : args) { system.out.println(arg); } } @around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(anndemo)") public object testbeforenote(proceedingjoinpoint joinpoint) throws throwable { //用的最多通知的签名 signature signature = joinpoint.getsignature(); methodsignature msg=(methodsignature) signature; object target = joinpoint.gettarget(); //获取注解标注的方法 method method = target.getclass().getmethod(msg.getname(), msg.getparametertypes()); //通过方法获取注解 anndemo annotation = method.getannotation(anndemo.class); object proceed; //获取参数 object[] args = joinpoint.getargs(); system.out.println(annotation.value()); system.out.println(annotation.isaop()); for (object arg : args) { system.out.println(arg); } if (objects.isnull(annotation) || !annotation.isaop()) { system.out.println("无需处理"); proceed = joinpoint.proceed(); }el { system.out.println("进入aop判断"); proceed = joinpoint.proceed(); if(proceed instanceof list){ list proceedlst = (list) proceed; if(!collectionutils.impty(proceedlst)){ tborder tborder = new tborder(); tborder.tpaymenttype("fffffffffffffffffff"); arraylist<tborder> tborderlst = new arraylist<>(); tborderlst.add(tborder); return tborderlst; } } system.out.println(proceed); } return proceed; }}
@target(elementtype.method)@retention(retentionpo李小凯licy.runtime)public @interface systemlog { public string description() default "";}
@responbody@validrequestbody@requestmapping("/login")@systemlog(description="登录")public globalrespon login(@requestbody @valid ur ur, bindingresult bindingresult){ ......}
@around("@annotation(com.xxx.xxx.xxx.systemlog)")public object around(proceedingjoinp武汉教师资格证oint joinpoint) throws throwable{ systemlog systemlog = ((methodsignature)joinpoint.getsignature()).getmethod().getannotation(systemlog.class); ......}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 13:42:16,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/4d8c8ce7b68fa0f55ff6fe4ebde6c8e4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:关于aop切面 注解、参数如何获取.doc
本文 PDF 下载地址:关于aop切面 注解、参数如何获取.pdf
留言与评论(共有 0 条评论) |