自定义注解,将业务模块中某个字段值记录到日志中
作者:互联网
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 }
标签:段值,java,自定义,objEntity,Object,field,FieldRemark,import,日志 来源: https://www.cnblogs.com/person008/p/15813480.html