码迷,mamicode.com
首页 > 编程语言 > 详细

Spring AOP面向切面编程,监听某个方法

时间:2018-06-05 18:40:50      阅读:1886      评论:0      收藏:0      [点我收藏+]

标签:ash   not   server   get   after   ons   key   success   coder   

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面向切面编程,监听某个方法

标签:ash   not   server   get   after   ons   key   success   coder   

原文地址:https://www.cnblogs.com/ouyanxia/p/9141094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!