spring

1.编写aop注意先将切面类注入容器(@Componment),然后加入@Aspest注解标注这个类是切面类,对那些方法进行增强@pointCut("execution"),怎么增强(@增强的位置),

a.用joinPoint获取签名,获取类名方法名等信息

b.如果想要获取被增强方法中的异常对象或者返回值则需要在方法的参数上增加一个对应类型的参数,并且使用注解的属性进行配置例如:@增强的位置(.return,thowed)

c.ProceedingJoinPonint环绕通知比较好用

调用proceed方法可以用来让目标方法执行增强

2.利用aop实现日志打印接口信息功能

a.自定义注解@SystemLog

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SystemLog {
    String businessName();
}

 将该注解加到我们想要实现该功能的方法上

  @PutMapping("/userInfo")
    @SystemLog(businessName = "更新用户信息")
    public ResponseResult updateUserInfo(@RequestBody User user){
        return iSysUserService.updateUserInfo(user);
    }

b.自定义切面类,加注解@Compent和@Aspect

@Component
@Aspect
@Slf4j
public class LogAspect {
}

配置切点

@Pointcut("@annotation(com.myblog.annotation.SystemLog)")
    public void pt() {

    }

环绕通知的配置

@Around("pt()")
    public Object printLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object ret;
        try {
            handleBefore(joinPoint);
            ret = joinPoint.proceed();
            handleAfter(ret);
        } finally {
            log.info("----End----"+System.lineSeparator());
        }
        return ret;
    }

    private void handleAfter(Object ret) {

        // 打印出参
        log.info("Response       : {}", JSON.toJSONString(ret));
        // 结束后换行
        log.info("=======End=======" + System.lineSeparator());
    }

    private SystemLog getSystemLog(ProceedingJoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        SystemLog annotation = signature.getMethod().getAnnotation(SystemLog.class);
        return annotation;

    }

    private void handleBefore(ProceedingJoinPoint joinPoint) {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取被增强方法上的注解对象
        SystemLog systemLog = getSystemLog(joinPoint);

        log.info("=======Start=======");
        // 打印请求 URL
        log.info("URL            : {}",request.getRequestURL());
        // 打印描述信息
        log.info("BusinessName   : {}", systemLog.businessName());
        // 打印 Http method
        log.info("HTTP Method    : {}", request.getRequestURL());
        // 打印调用 controller 的全路径以及执行方法
        log.info("Class Method   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), ((MethodSignature) joinPoint.getSignature()).getName());
        // 打印请求的 IP
        log.info("IP             : {}",request.getRemoteHost());
        // 打印请求入参
        log.info("Request Args   : {}", JSON.toJSONString(joinPoint.getArgs()));
        // 结束后换行
        log.info("=======End=======" + System.lineSeparator());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值