1. 程式人生 > >Spring Boot使用AOP實現攔截某個方法

Spring Boot使用AOP實現攔截某個方法

ces 文件 commit ransac dex red code join info

1、引入.jarl文件依賴

        <!-- Spring Boot Web 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 從依賴信息裏移除 Tomcat配置 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2、引入aop相關的jar

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

3、編寫Interceptor攔截類,兩種方式實現

@Aspect
@Component
public class CallBackLogInterceptor {
    private
Logger logger = LoggerFactory.getLogger(CallBackLogInterceptor.class); @Autowired private PlatformTransactionManager platformTransactionManager;//配置事務管理 @Autowired private CallBackLogMapper callBackLogMapper; protected TransactionStatus startTx() { TransactionStatus transactionStatus
= platformTransactionManager .getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW)); return transactionStatus; } protected void commitTx(TransactionStatus transactionStatus){ if(transactionStatus != null){ platformTransactionManager.commit(transactionStatus); } } protected void rollbackTx(TransactionStatus transactionStatus){ if (transactionStatus != null) { platformTransactionManager.rollback(transactionStatus); } } @AfterReturning(pointcut = "execution(* com.gcard.notify.sevice.impl.NotifyCashManager.notifyCash(..))&&args(notifyUrl, cashId, reqSn, callResult)", returning = "resultValue") public void handleInsertCallBackLog(String resultValue,String notifyUrl, String cashId, String reqSn, String callResult){ try { logger.info(cashId); logger.info(resultValue); String result = StringUtils .lessWord(resultValue, 1000 - 3); String type = SuperAppConstant.CASH_CALL_BACK_TYPE; int notifyStatus = SuperAppConstant.NOTIFY_STATUS_SUCCESS; addCallBackLog(cashId,notifyUrl,result,type,notifyStatus); } catch (Exception e) { logger.error(e.getMessage(),e); } } @AfterThrowing(pointcut = "execution(* com.gcard.notify.sevice.impl.NotifyCashManager.notifyCash(..))&&args(notifyUrl, cashId, reqSn, callResult)", throwing="ex") public void handleUpdateCashAfterThrow(Throwable ex,String notifyUrl, String cashId, String reqSn, String callResult){ try { logger.info(cashId); logger.info(ex.getMessage()); String result = StringUtils .lessWord(ex.getMessage(), 1000 - 3); String type = SuperAppConstant.CASH_CALL_BACK_TYPE; int notifyStatus = SuperAppConstant.NOTIFY_STATUS_FAIL; addCallBackLog(cashId, notifyUrl, result, type, notifyStatus); } catch (Exception e) { logger.error(e.getMessage(), e); } } }
    @Pointcut("execution(* com.gmall88.server.manager.superapp.SuperAppServerManager.addDo*(..))")
    public void addDo(){
        
    }
@AfterReturning(pointcut = "addDo()", returning = "resultValue")
    public void addDoPayAfterReturning(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.SUCCESS.getCode()){
                    msg = result.getMessage();
                }
            }else{
                if(resultValue !=null){
                    msg = resultValue.toString();
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

註意:攔截的方法要是一個bean調用才能生效,比如:攔截的notifyCash()方法,需要獨立在一個類,然後被其他類調用

@Component
public class NotifyCashManager {
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    public String notifyCash(String notifyUrl, String cashId, String reqSn, String callResult) throws Exception {
        String P = MD5.MD5Encode(cashId + "" + SuperAppConstant.NOTIFY_TOKEN);

        callResult = URLEncoder.encode(callResult, "UTF-8");
        String param = String.format("?Log_id=%s&P=%s&Req_Sn=%s&Cash_Result=%s", cashId, P, reqSn, callResult);
        String requestUrl = notifyUrl + param;
        logger.info("param=" + requestUrl);
        net.sf.json.JSONObject jsonObject = RF.httpsRequestJson(requestUrl, "POST", "");
        if (jsonObject == null) {
            String msg = null;
            msg = "Error to call notifyUrl.";
            logger.error(msg);
            throw new ExceptionVo(msg);
        } else {
            String msg = jsonObject.toString();
            logger.info("jsonObject=" + msg);
            if (jsonObject.containsKey("code")) {
                int code = jsonObject.getInt("code");
                if (code != SuperAppConstant.NOTIFY_RETURN_CODE_SUCCESS) {
                    String message = jsonObject.getString("message");
                    logger.error(message);
                    throw new ExceptionVo(message);
                }
            }
            return msg;
        }
    }
}

被調用:

@Autowired
private NotifyCashManager notifyCashManager;

notifyCashManager.notifyCash(notifyUrl, cashId, reqSn, callResult);//調用方式

Spring Boot使用AOP實現攔截某個方法