自定义方法适用于 aop 切面方法监听,不建议使用于解析方法或者反射方法执行。
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordAnnotation {
NursingRecordOperationType saveLogType() default NursingRecordOperationType.ADD;
}
使用 AOP 切面示例
@Aspect
@Component
@Slf4j
public class RecordAspect {
private final NursingRecordLogService nursingRecordLogService;
public RecordAspect(NursingRecordLogService nursingRecordLogService) {
this.nursingRecordLogService = nursingRecordLogService;
}
@Pointcut("@annotation(com.depth.RecordAnnotation)")
private void saveLog() {
}
@Around("saveLog() && @annotation(recordAnnotation)")
public Object doSaveLog(ProceedingJoinPoint joinPoint, RecordAnnotation recordAnnotation) throws Throwable {
// 获取方法返回值
Object proceed = joinPoint.proceed();
// 返回值验证
if (Objects.isNull(proceed)) {
return null;
}
try {
// 保存类型枚举
NursingRecordOperationType nursingRecordOperationType = recordAnnotation.saveLogType();
// 构建日志信息保存
NursingRecordVo nursingRecordVo = new NursingRecordVo();
nursingRecordVo.setOperationType(nursingRecordOperationType);
。。。。。。
} catch (Exception e) {
log.error("日志保存失败.2--type:" + recordAnnotation.saveLogType().getValue(), e);
}
return proceed;
}