Sping Boot 自定义注解使用示例-自定义方法注解

自定义方法适用于 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;
    }
上一篇:移动零


下一篇:【MATLAB】基于RSSI的蓝牙定位程序,4个锚点、二维平面