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 }