Spring AOP面向切面编程,监听某个方法
Posted 依米欧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring AOP面向切面编程,监听某个方法相关的知识,希望对你有一定的参考价值。
1、单独监听某一个方法,方法的参数名称必须与args定义的参数名称一致
@AfterReturning(pointcut = "execution(* com.gmall88.server.manager.superapp.SuperAppServerManager.notifyRefund(..))&&args(notifyUrl, refundId, batchNo, callResult)", returning = "resultValue") public void handleInsertCallBackLogNotifyRefund(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; int notifyStatus = 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 void handleNotifyRefundAfterThrow(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; int notifyStatus = SuperAppConstant.NOTIFY_STATUS_FAIL; superAppServerManager.addCallBackLog(refundId, notifyUrl, result, type, notifyStatus); } catch (Exception e) { logger.error(e.getMessage(), e); } }
2、监听多个方法,可以是某个方法的开头(以handle开头命名的方法)
// handleCash @Pointcut("execution(* com.gmall88.server.manager.superapp.SuperAppServerManager.handle*(..))") public void handleCash(){ } @Pointcut("execution(* com.gmall88.server.manager.superapp.SuperAppServerManager.addDo*(..))") public void addDo(){ } @Pointcut("execution(* com.gmall88.server.manager.superapp.SuperAppServerManager.handleRetry*(..))") public void handleRetry(){ } @AfterReturning(pointcut = "handleCash() || handleRetry()", returning = "resultValue") public void handleCashAfterReturning(JoinPoint jp, Object resultValue) { try { Object[] parames = jp.getArgs();// 获取目标方法体参数 String params = parseParames(parames); // 解析目标方法体的参数 String className = jp.getTarget().getClass().toString();// 获取目标类名 className = className.substring(className.indexOf("com")); logger.info("params" + params); String signature = jp.getSignature().toString();// 获取目标方法签名 String methodName = signature.substring(signature.lastIndexOf(".") + 1, signature.indexOf("(")); logger.info("methodName" + methodName); ReturnResult result = new ReturnResult(); String msg= ""; int code = -1; if(resultValue instanceof Map){ Map map = (Map) resultValue; if(map.containsKey("errorMsg")){ msg= (String) map.get("errorMsg"); } }else if(resultValue instanceof ReturnResult){ 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 = new HashMap(); map.put("code", code); map.put("params", params); map.put("message", res); JSONObject obj = JSONObject.fromObject(map); String type ="AfterReturning:"; String objString = new String(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面向切面编程,监听某个方法的主要内容,如果未能解决你的问题,请参考以下文章