Spring Aop 记录用户操作日志

本文介绍了如何利用Spring AOP和自定义注解来记录用户操作日志,包括创建自定义注解、定义需要记录操作的模块、设置切面类以及进行测试。虽然文中没有实际的用户操作数据,但提供了相关参考博客以供进一步学习。
摘要由CSDN通过智能技术生成

前言

记录用户全流程操作日志,网上搜了搜,参考着写了下,使用Spring Aop和自定义注解完成,参考博客见最后,感谢。

正文

自定义注解
/**
 * @program: springaop
 * @description 自定义注解,作用于方法,运行时有效
 * @author: jjh
 * @create: 2020-05-16 
 **/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface LogAnnotation {
    //模块名称
    String moudleName() default "";
    //操作内容
    String option() default "";
}
要记录操作的模块

定义一个用户管理模块接口UserManager

/**
 * @program: springaop
 * @description
 * @author: jjh
 * @create: 2020-05-16
 **/
public interface UserManager {
    /**
     * 添加了一个用户
     */
    public void addUser();

    /**
     * 添加一个角色
     */
    public void addRole();
}

实现类 UserManagerImpl


/**
 * @program: springaop
 * @description
 * @author: jjh
 * @create: 2020-05-16
 **/
@Component("usrmanageraction")
public class UserManagerImpl implements UserManager {

    @LogAnnotation(moudleName = "用户管理模块", option = "添加用户")
    @Override
    public void addUser() {
        //假装执行了用户添加操作
        System.out.println("添加了一个用户");
    }

    @LogAnnotation(moudleName = "用户管理模块", option = "添加用户")
    @Override
    public void addRole() {
        //假装添加了一个角色
        System.out.println("添加了一个角色");
    }
}
定义切面类
/**
 * @program: springaop
 * @description 切面类,用于记录用户操作后日志
 * @author: jjh
 * @create: 2020-05-16  
 **/
@Aspect
@Component
public class LogInterceptor {

    /**
     * 定义用户管理模块的切点
     */
    @Pointcut("execution(* com.jjh.testoperation..*.*(..))")
    public void userManagerPointcut(){
    }

    @After("userManagerPointcut()")
    public void afterLog(JoinPoint joinPoint) throws NoSuchMethodException {
        //用的最多通知的签名
        Signature signature = joinPoint.getSignature();
        MethodSignature msg=(MethodSignature) signature;
        Object target = joinPoint.getTarget();
        //获取注解标注的方法
        Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
        //通过方法获取到自定义的注解
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);

        //记录日志 看实际情况,日志存表入库
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date())
                + "  " + annotation.moudleName() + "  " + annotation.option());

    }
}
测试
/**
 * @program: springaop
 * @description
 * @author: jjh
 * @create: 2020-05-16
 **/
public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManagerImpl userManager = (UserManagerImpl) ac.getBean("usrmanageraction");
        userManager.addRole();
        userManager.addUser();
    }
}

在这里插入图片描述

由于我这就是几个java文件,没有用户,所以也就不能记录了,记录用户可参考最后的第一个参考博客。

最后

参考博客:

使用Spring AOP使用注解记录用户操作日志

aop切面 注解、参数获取方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值