spring boot 项目之-引入AOP切面

(结合自定义注解,实现日志的处理)

1.引入依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>

2.创建自己的自定义注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BussinessLog {
    /** 操作类型: 新增 修改 删除 ,赋值要采用枚举,便于后期日志的整理归档 **/
    String operateType() default "";
    /** 功能模块 来源: 最好采用菜单的唯一性标志,便于的归档 **/
    String sourceList() default "";
    /** 操作信息 **/
    String msg() default "";
}

2.创建切面


@Slf4j
@Aspect
@Component
public class BusinessLogAspect {
    //利用注解 配置织入点
    @Pointcut("@annotation(com.qinzhy.mydemo.annotation.BussinessLog)")
    public void annotationPointCut(){}

    /**
     * 后置通知
     */
    @After("annotationPointCut()")
    public void insertLogByAnnotion(JoinPoint joinPoint){
        log.info(JSON.toJSONString("后置通知 : "+joinPoint));
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
        //获取注解的信息
        String operateType = bussinessLog.operateType();
        String sourceList = bussinessLog.sourceList();
        String msg = bussinessLog.msg();
        log.info("后置方法式AOP拦截 【{}】" , operateType);
        log.info("后置方法式AOP拦截 【{}】" , sourceList);
        log.info("后置方法式AOP拦截 【{}】" , msg);


        //获取session中用户的信息

        //保存日志信息


    }

    /**
     * 前置通知
     */
    @Before("execution(* com.qinzhy.mydemo.controller.TestAspectController.*(..))")
    public void beforeLogByAnnotion(JoinPoint joinPoint){
        log.info(JSON.toJSONString("前置通知 : "+ joinPoint));
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
        //获取注解的信息
        String operateType = bussinessLog.operateType();
        String sourceList = bussinessLog.sourceList();
        String msg = bussinessLog.msg();
        log.info("方法式AOP拦截 【{}】" , operateType);
                                                                                                                                                    log.info("后置异常注解式AOP拦截 【{}】" , sourceList);
        log.info("方法式AOP拦截 【{}】" , msg);
        //获取session中用户的信息

        //保存日志信息

    }


    /**
     * 异常日志处理
     */
    @AfterThrowing("annotationPointCut()")
    public void exceptionLogByAnnotion(JoinPoint joinPoint){
        log.info(JSON.toJSONString("前置通知 : "+ joinPoint));
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
        //获取注解的信息
        String operateType = bussinessLog.operateType();
        String sourceList = bussinessLog.sourceList();
        String msg = bussinessLog.msg();
        log.info("异常捕获注解式AOP拦截 【{}】" , operateType);
        log.info("异常捕获注解式AOP拦截 【{}】" , sourceList);
        log.info("异常捕获注解式AOP拦截 【{}】" , msg);
        //获取session中用户的信息

        //保存日志信息

    }

}

3.使用该自定义的日志注解

/**
 * 测试利用切面进行日志记录
 */
@BussinessLog(operateType = "add",sourceList = "新增列表", msg = "测试注解式拦截")
@GetMapping("/add")
public String testAspect(){

    System.out.println("执行中>>>>>>>>>>>>>");
    return  "success";
}

4.实际使用案例

异常日志处理 或者成功后日志处理:未加入的安全模块以及session处理

@Slf4j
@Aspect
@Component
public class BusinessLogAspect {
    //利用注解 配置织入点
    @Pointcut("@annotation(com.qinzhy.mydemo.annotation.BussinessLog)")
    public void annotationPointCut(){}

    /**
     *  后置通知
     * @param joinPoint 切点
     * @param jsonResult 返回的参数
     */
    @AfterReturning( pointcut = "annotationPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult){
        log.info(JSON.toJSONString("成功日志处理通知 : "+joinPoint));
        handleLog(joinPoint, null,  jsonResult);

    }



    /**
     * 返回的异常
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value = "annotationPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e){
        log.info(JSON.toJSONString("异常拦截通知 : "+ joinPoint));
        handleLog(joinPoint, e,  null);
    }

    /**
     * 日志处理
     * @param joinPoint 切点
     * @param e 异常
     * @param jsonResult 返回的参数
     */
    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult){
        //获取注解
        BussinessLog bussinessLog = this.getAnnotationLog( joinPoint);
        if(bussinessLog == null){
            return;
        }
        //获取注解的信息
        String operateType = bussinessLog.operateType();
        String sourceList = bussinessLog.sourceList();
        String msg = bussinessLog.msg();
        log.info("注解式AOP拦截 【{}】" , operateType);
        log.info("注解式AOP拦截 【{}】" , sourceList);
        log.info("注解式AOP拦截 【{}】" , msg);

        //获取用户信息


        //获取请求参数信息  ip  等


        // 返回参数
        if(jsonResult != null){
            //赋值到返回参数存放字段里

        }
        //根据Exception e 是否存在来判断是否执行成果
        if ( e != null){
            //设置执行失败的状态

            //设置失败的原因 e.getMessage();

        }else{
            //设置执行成功的状态

        }

        //调用保存到数据库的方法



    }


    /**
     * 获取注解里的信息
     * @param joinPoint
     * @return BussinessLog
     */
    private BussinessLog getAnnotationLog(JoinPoint joinPoint){
        joinPoint.getSignature();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if(method != null){
            return method.getAnnotation(BussinessLog.class);
        }
        return null;

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamCatcher-qin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值