zoukankan      html  css  js  c++  java
  • AOP记录日志

    1.自定义注解

    @Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
    @Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
    @Documented //生成文档
    public @interface RptLog {
        String value() default "";
    }

    2.aop相关的配置类

    @Slf4j
    @Aspect
    @Component
    public class RptOperateLogAspect {
    
        @Autowired
        @SuppressWarnings("all")
        private RptOperateLogMapper rptOperateLogMapper;
    
        @Autowired
        private UserService userService;
        //定义切点 @Pointcut
        //在注解的位置切入代码
        @Pointcut("@annotation(com.in.g.data.config.RptLog)")
        public void logPoinCut() {
        }
    
        //切面 配置通知
        @AfterReturning("logPoinCut()")
        public void saveSysLog(JoinPoint joinPoint) {
            System.out.println("报表系统记录操作日志");
            //保存日志
            RptOperateLog rptOperateLog = new RptOperateLog();
    
            //从切面织入点处通过反射机制获取织入点处的方法
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取切入点所在的方法
            Method method = signature.getMethod();
    
            //获取当前用户操作信息
            Object[] args = joinPoint.getArgs();
            if (ArrayUtils.isNotEmpty(args)){
                Integer id = (Integer) args[0];
                //记录操作用户的信息
                SessionUserDTO user = userService.getUser(id);
                rptOperateLog.setRole(user.getRoleName());
                rptOperateLog.setUsername(user.getFullName());
                rptOperateLog.setAccount(user.getUserName());
            }
            RptLog rptLog = method.getAnnotation(RptLog.class);
            if (rptLog != null) {
                String value = rptLog.value();
                //操作名称
                rptOperateLog.setOperateContent(value);
            }
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            //操作时间
            rptOperateLog.setOperateTime(new Date());
            //操作用户ip地址
            rptOperateLog.setIpAddress(IpAddressUtil.getIpAddr(request));
            //操作设备信息
            String deviceInfo = AgentUserKit.getDeviceInfo(request);
            rptOperateLog.setDevice(deviceInfo);
            //保存操作日志
            rptOperateLogMapper.saveLog(rptOperateLog);
            log.info("报表日志保存成功 rptOperateLog:[{}]",rptOperateLog);
        }
    }

    3.使用自定义注解

        @GetMapping("/customer_info")
        @RptLog(value = "下载-客户详情报表")
        public Result customerInfoExcelGenerate(@RequestHeader(CommonConstant.X_OPERATOR_ID) Integer loginId,
                                                @Validated  CustomerInfoDTO customerInfoDTO) {
            iReportExcelRecordService.customerInfoExcelGenerate(loginId,customerInfoDTO);
            return Result.success(null);
        }
  • 相关阅读:
    网页加速的14条优化法则 网站开发与优化
    .NET在后置代码中输入JS提示语句(背景不会变白)
    C语言变量声明内存分配
    SQL Server Hosting Toolkit
    An established connection was aborted by the software in your host machine
    C语言程序设计 2009春季考试时间和地点
    C语言程序设计 函数递归调用示例
    让.Net 程序脱离.net framework框架运行
    C语言程序设计 答疑安排(2009春季 110周) 有变动
    软件测试技术,软件项目管理 实验时间安排 2009春季
  • 原文地址:https://www.cnblogs.com/july-sunny/p/12057097.html
Copyright © 2011-2022 走看看