zoukankan      html  css  js  c++  java
  • 自定义注解,将业务模块中某个字段值记录到日志中

    1、定义注解

    package com.zhhs.framework.aspectj;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface FieldRemark {
        public String value() default "";
    }

    2、将注解加到需要记录的字段上方

     /** 新闻ID */
     @FieldRemark("id")
      private Long newsId;

    3、在切面中取值

      public Object getArgsEntity(JoinPoint joinPoint) {
            Object[] arguments = joinPoint.getArgs();
            Object objEntity = null;
            if (arguments.length > 0) {
                for (Object arg : arguments) {
                    if (arg instanceof BaseEntity) {
                        objEntity = arg;
                    }
                }
            }
            return objEntity;
        }
     1 OperLog operLog = new OperLog();
     2 operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
     3 Object objEntity = getArgsEntity(joinPoint);
     4 Field fields[] = objEntity.getClass().getDeclaredFields();
     5 for (Field field : fields) {
     6     field.setAccessible(true);
     7     FieldRemark fieldRemark = field.getAnnotation(FieldRemark.class);
     8     if (fieldRemark != null){
     9           operLog.setModuleId(Long.valueOf(field.get(objEntity).toString()));
    10     }
    11 }
  • 相关阅读:
    呵呵
    数据类型转换方法
    工业设计三原则
    C#实现的根据年月日计算星期几的函数
    网页设计的12种颜色
    SqlParameter 存储过程
    HTTP 状态响应码
    Android获取屏幕高度和宽度
    Android屏幕自适应解决方案
    Nodejs学习笔记nodejs的安装
  • 原文地址:https://www.cnblogs.com/person008/p/15813480.html
Copyright © 2011-2022 走看看