springboot aspect中@Pointcut 和@Around是什么,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1、背景
基于springboot2.X版本
結合 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");
//定義表達式 此處使用within 特別注意esLog不用全路徑 因為此處它是個參數
private final String POINT_CUT = "within(com.ysma.appliaction.service..*) && @annotation(esLog)";
//定義pointcut pj不用管,默認必須且為第一位
@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){//運行時異常
Exception ex = (Exception)obj;
eld.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
eld.setErrorMsg(ex.getMessage());
} else {//無異常
eld.setRes(obj.toString());
}
}
break;
case H5://TODO 待擴展
default:
break;
}
return eld;
}
}6、針對如下錯誤:
IllegalArgumentException: error at ::0 formal unbound in pointcut
Error creating bean with name 'tomcatServletWebServerFactory
看完上述內容,你們掌握springboot aspect中@Pointcut 和@Around是什么的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。