AOP日志记录

AOP日志

AOP日志就是简单记录一些用户执行的过程
  1. 导入依赖包
<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
  1. 自定义注解LogAnno
//声明执行时间,runtime运行时
@Retention(RetentionPolicy.RUNTIME)
//声明作用域,method作用在方法上
@Target(ElementType.METHOD)

public @interface LogAnno {

    //自定义的两个变量
    String operationType();

    String operationName();
}
  1. 接下来就是切面表达式,通知

@Aspect
@Component
public class LogUtils {

    /**
     * 切入点表达式 *代表一个目录   ..代表两个或多个
     */
    @Pointcut("execution(* com.qianfeng..controller..*(..))")
    public void pointcut() {

    }

    /**
     * 后置通知
     * @param joinPoint
     * @param o
     * @throws ClassNotFoundException
     */
    @AfterReturning(pointcut = "pointcut()", returning = "o")
    public void after(JoinPoint joinPoint, Object o) throws ClassNotFoundException {
        //获取类的全名称
        String classname = joinPoint.getTarget().getClass().getName();

        //获取方法上的注解
        Class<?> aClass = Class.forName(classname);

        //获取类中的方法
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            //得到方法上的LogAnno注解
            LogAnno logAnno = method.getAnnotation(LogAnno.class);

            if (logAnno != null) {

                String operationName = logAnno.operationName();
                String operationType = logAnno.operationType();

                //获得用户
                User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");

                System.out.println(operationName + "----" + operationType + "被执行了,执行者" + user.getUser_name());
            }
        }
    }

    /**
     * 异常通知
     * @param joinPoint
     * @param t
     * @throws ClassNotFoundException
     */
    @AfterThrowing(pointcut = "pointcut()", throwing = "t")
    public void throwing(JoinPoint joinPoint,Throwable t) throws ClassNotFoundException {
        //获取类的全名称
        String classname = joinPoint.getTarget().getClass().getName();

        //获取方法上的注解
        Class<?> aClass = Class.forName(classname);

        //获取类中的方法
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            //得到方法上的LogAnno注解
            LogAnno logAnno = method.getAnnotation(LogAnno.class);

            if (logAnno != null) {

                String operationName = logAnno.operationName();
                String operationType = logAnno.operationType();

                //获得用户
                User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");

                System.out.println(operationName + "----" + operationType + (user==null?"":"被执行了,执行者" + user.getUser_name())+"出现异常了");
            }
        }
    }
}

这样就可以了!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值