温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot aspect中@Pointcut 和@Around是什么

发布时间:2021-10-20 10:17:42 来源:亿速云 阅读:405 作者:柒染 栏目:大数据

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI