Spring AOP Programming for facets, listening to a method

Source: Internet
Author: User
Tags aop

1, listen to a method alone, the parameter name of the method must be the same as the parameter name of the args definition

@AfterReturning (pointcut = "Execution (* com.gmall88.server.manager.superapp.SuperAppServerManager.notifyRefund (..)) &&args (Notifyurl, Refundid, Batchno, Callresult) ", returning =" Resultvalue ")     Public voidhandleinsertcallbacklognotifyrefund (String resultvalue,string notifyurl, String refundid, String batchno,string Callresult) {Try{logger.info ("Refundid" +Refundid);            Logger.info (Resultvalue); String result=com.gmall88.server.util.StringUtils. Lessword (Resultvalue,1000-3); String type=Superappconstant.refund_call_back_type; intNotifystatus =superappconstant.notify_status_success;        Superappservermanager.addcallbacklog (Refundid,refundid,result,type,notifystatus); } Catch(Exception e) {logger.error (E.getmessage (), E); }} @AfterThrowing (Pointcut= "Execution (* com.gmall88.server.manager.superapp.SuperAppServerManager.notifyRefund (..)) &&args (Notifyurl, Refundid, Batchno, Callresult) ", throwing=" ex ")     Public voidHandlenotifyrefundafterthrow (Throwable ex,string notifyurl, String refundid, String batchno,string callResult) { Try{logger.info ("Handlenotifyrefundafterthrow Refundid:" +Refundid);            Logger.info (Ex.getmessage ()); String result=com.gmall88.server.util.StringUtils. Lessword (Ex.getmessage (),1000-3); String type=Superappconstant.refund_call_back_type; intNotifystatus =Superappconstant.notify_status_fail;        Superappservermanager.addcallbacklog (Refundid, Notifyurl, result, type, notifystatus); } Catch(Exception e) {logger.error (E.getmessage (), E); }    }

2, listen to more than one method, can be the beginning of a method (named after the beginning of the handle method)

//Handlecash@Pointcut ("Execution (* com.gmall88.server.manager.superapp.superappservermanager.handle* (..))")     Public voidHandlecash () {} @Pointcut ("Execution (* com.gmall88.server.manager.superapp.superappservermanager.adddo* (..))")     Public voidAdddo () {} @Pointcut ("Execution (* com.gmall88.server.manager.superapp.superappservermanager.handleretry* (..))")     Public voidhandleretry () {} @AfterReturning (Pointcut= "Handlecash () | | | Handleretry () ", returning =" Resultvalue ")     Public voidhandlecashafterreturning (joinpoint JP, Object resultvalue) {Try{object[] parames= Jp.getargs ();//get target method body parameterString params = parseparames (parames);//parse the parameters of the target method bodyString className = Jp.gettarget (). GetClass (). toString ();//Gets the target class nameClassName = classname.substring (classname.indexof ("com")); Logger.info ("Params" +params); String Signature= Jp.getsignature (). toString ();//get target method signatureString methodName = signature.substring (Signature.lastindexof (".") + 1, Signature.indexof ("(")); Logger.info ("MethodName" +methodName); Returnresult result=NewReturnresult (); String msg= ""; intCode =-1; if(ResultvalueinstanceofMap) {Map Map=(MAP) Resultvalue; if(Map.containskey ("ErrorMsg") ) {msg= (String) map.get ("ErrorMsg"); }            }Else if(ResultvalueinstanceofReturnresult) {Result=(Returnresult) Resultvalue; Code=Result.getcode (); if(Code = =ReturnCodeType.NOT_FOUND.getCode ()) {msg=Result.getmessage (); }            }Else{                if(Resultvalue! =NULL) {msg=resultvalue.tostring (); }            }            if(Stringutils.isnotblank (msg)) {String res= Com.gmall88.server.util.StringUtils.lessWord (msg, 1000-3); Map Map=NewHashMap (); Map.put ("Code", code); Map.put ("Params", params); Map.put ("Message", RES); Jsonobject obj=jsonobject.fromobject (map); String type= "Afterreturning:"; String objstring=NewString (Obj.tostring (). GetBytes ("UTF-8")); String objUTF8= Urlencoder.encode (objstring, "UTF-8");            SUPERAPPSERVERMANAGER.ADDJMSSENDEMAILTOMQ (Classname,methodname,objstring,type); }        } Catch(Exception e) {logger.error (E.getmessage (), E); }    }

Spring AOP Programming for facets, listening to a method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.