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 }

 

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-07-06
  • 2022-12-23
  • 2021-05-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-06-21
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
相关资源
相似解决方案