千家信息网

springboot aspect中@Pointcut 和@Around是什么

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,springboot aspect中@Pointcut 和@Around是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1、背景基于
千家信息网最后更新 2025年12月02日springboot aspect中@Pointcut 和@Around是什么

springboot aspect中@Pointcut 和@Around是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1、背景

基于springboot2.X版本

结合 https://my.oschina.net/ysma1987/blog/597601 第二版更新

2、针对AOP切面

            org.springframework.boot            spring-boot-starter-aop        

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@Componentpublic 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是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0