Spring实现日志切面
发布日期:2021-06-29 11:46:54 浏览次数:3 分类:技术文章

本文共 2853 字,大约阅读时间需要 9 分钟。

AOP相关注解介绍

@Aspect:作用是把当前类标识为一个切面供容器读取@Pointcut:Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。@Around:环绕增强,相当于MethodInterceptor@AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行@Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有@AfterThrowing:异常抛出增强,相当于ThrowsAdvice@After: final增强,不管是抛出异常或者正常退出都会执行

@Pointcut的用法

//表示匹配所有方法  1)execution(* *(..))  //表示匹配com.xl.server.UserService中所有的公有方法  2)execution(public * com. xl.service.UserService.*(..))  //表示匹配com.xl.server包及其子包下的所有方法3)execution(* com.xl.server..*.*(..))  //表示匹配注解SysLog4)@annotation(com.platform.annotation.SysLog)

自定义注解

package com.hnac.hzinfo.logger.entity;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyLog {    //日志标题    public String title() default "";}

日志切面

package com.hnac.hzinfo.logger.support;import com.fasterxml.jackson.core.JsonProcessingException;import com.hnac.hzinfo.logger.annotation.Operate;import com.hnac.hzinfo.logger.constant.BusinessStatusEnum;import com.hnac.hzinfo.logger.entity.Operation;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.time.LocalDateTime;import java.time.ZoneId;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.UUID;@Aspect@Componentpublic class MyLogAspect {    @Pointcut("@annotation(com.hnac.hzinfo.logger.entity.MyLog)")    public void logPointCut(){    }    @AfterReturning(pointcut = "logPointCut()")    public void doAfterReturning(JoinPoint point) throws JsonProcessingException {        recordMyLog(point, null);    }    private void recordMyLog(final JoinPoint point, final Exception e) throws JsonProcessingException {        System.out.println("自定义注解记录日志");    }    @AfterThrowing(value = "logPointCut()",throwing = "e")    public void doAfterReturning(JoinPoint point,Exception e) throws JsonProcessingException {        System.out.println("自定义注解记录日志-异常时进入");    }}

使用自定义注解

@Override    @MyLog(title = "自定义日志切面")    public int addGate(IrrBDGate irrBDGate) {        int i = 0;        //判断闸门编码重复        IrrBDGate juggeGate = irrBDGateMapper.selectByPrimaryKey(irrBDGate.getStrobeCode());        if(juggeGate != null){            throw new RuntimeException("存在重复的闸门编码为:"+irrBDGate.getStrobeCode()+"的数据");        }        i = irrBDGateMapper.insertSelective(irrBDGate);        return i;    }

 

日志切面参考了文章:

转载地址:https://blog.csdn.net/zzchances/article/details/104898573 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:flowable多实例
下一篇:Spring单元测试

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月07日 04时19分16秒