千家信息网

springboot aspect中@Pointcut 和@Around是什么

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,springboot aspect中@Pointcut 和@Around是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1、背景基于
千家信息网最后更新 2025年02月04日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是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

不用 内容 方法 更多 问题 束手无策 为此 代码 切面 原因 参数 对此 技能 日志 版本 算法 篇文章 经验 背景 行业 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 北京软件开发合肥 结合自身经历说一说网络安全 每天一分钟网络安全 华为网络技术基础问题 无源光网络技术的背景 网络安全与执法研究生好就业吗 网络安全一百强 客户服务器程序设计实验报告 重庆应用软件开发哪家便宜 联通网络技术大会论文下载 灵汐科技互联网大会成果 药融云数据库从哪里看 逃离塔科夫哪些服务器挂少 网络安全群头街 库尔勒互联网科技 四川正规软件开发服务推广 eclipse怎么分离数据库 网络安全主要技术之一 数据库课程学习过程中遇到的问题 怎么买其他qq号数据库 去实业做软件开发有前途嘛 客户服务器程序设计实验报告 新疆星辰华盛网络技术有限公司 软件开发计入什么科目 学k8s和数据库哪个好 做网络安全解决方案 一个软件开发成功了怎样运行 软件开发语言是怎么产生的 中国重要报纸全文数据库汇总 数据库实例更改到底对
0