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