溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot aspect中@Pointcut 和@Around是什么

發(fā)布時間:2021-10-20 10:17:42 來源:億速云 閱讀:470 作者:柒染 欄目:大數(shù)據(jù)

springboot aspect中@Pointcut 和@Around是什么,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

1、背景

     基于springboot2.X版本

    結(jié)合 https://my.oschina.net/ysma1987/blog/597601 第二版更新

2、針對AOP切面

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

3、多說無益,直接上代碼

package com.ysma.webconfig.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ysma.entity.EsLogDto;
import com.ysma.exception.CustomException;
import com.ysma.intf.EsLog;
import com.ysma.util.SnowFlake;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class LogAspect {

    //定義ES日志
    private static final Logger LOGGER = LoggerFactory.getLogger("RES_REQ_LOG_ES_APPENDER");

    //定義表達(dá)式 此處使用within  特別注意esLog不用全路徑 因為此處它是個參數(shù)
    private final String POINT_CUT = "within(com.ysma.appliaction.service..*) && @annotation(esLog)";

    //定義pointcut  pj不用管,默認(rèn)必須且為第一位
    @Pointcut(value = POINT_CUT)
    public void pointCut(EsLog esLog){}

    //需與pointCut同,  argNames可去除
    @Around(value = "pointCut(esLog)", argNames = "pj, esLog")
    public Object around(ProceedingJoinPoint pj, EsLog esLog) throws Throwable {
        StopWatch sw = StopWatch.createStarted();
        EsLogDto dto = null;
        try {
            Object obj = pj.proceed();
            if(esLog.available()){
                dto = wrap(pj.getArgs(), esLog, obj);
            }
            return obj;
        } catch (Throwable ex) {
            if(esLog.available()){
                dto = wrap(pj.getArgs(), esLog, ex);
            }
            throw ex;
        } finally {
            sw.stop();
            if(dto != null){
                dto.setCostTime(sw.getTime(TimeUnit.MILLISECONDS));
                dto.setAddTime(new Date());
                LOGGER.info(dto.toString());
            }
        }
    }

    private EsLogDto wrap(Object[] args, EsLog esLog, Object... objs) {
        EsLogDto eld = new EsLogDto();
        switch (esLog.source()){
            case XFXJ: {
                String apiCode = (String)args[0];
                eld.setApiCode(apiCode);


                String apiParam = (String)args[1];
                eld.setReq(apiParam);
                JSONObject obJson = JSON.parseObject(apiParam);

                String id = obJson.getString("applySerialNo");
                if(id != null){
                    eld.setId(id);
                } else {
                    //備用雪花算法
                    eld.setId(SnowFlake.getInstance().nextId());
                }

                Object obj = objs[0];
                if(obj instanceof CustomException){//定制異常
                    CustomException ce = (CustomException)obj;
                    eld.setErrorCode(ce.getErrorCode());
                    eld.setErrorMsg(ce.getMessage());
                } else if(obj instanceof Exception){//運(yùn)行時異常
                    Exception ex = (Exception)obj;
                    eld.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
                    eld.setErrorMsg(ex.getMessage());
                } else {//無異常
                    eld.setRes(obj.toString());
                }
            }
                break;
            case H5://TODO 待擴(kuò)展
            default:
                break;
        }


        return eld;
    }
}

6、針對如下錯誤:

    IllegalArgumentException: error at ::0 formal unbound in pointcut 

    Error creating bean with name 'tomcatServletWebServerFactory

看完上述內(nèi)容,你們掌握springboot aspect中@Pointcut 和@Around是什么的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI