自定义注解使用

2021-06-21  本文已影响0人  Raral

同步业务使用

1.注解

@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckParams {

    public String title() default "";
}

  1. 切面
@Component
@Slf4j
@Aspect
public class CheckParamsAspect {


    //配置织入点
    @Pointcut("@annotation(com.gzsz.shop.api.common.annotation.CheckParams)")
    public void CheckParamsPointCut() {

    }

    // 执行前
    @Before("CheckParamsPointCut()")
    public void CheckParamsBefore(JoinPoint joinpoint) {
        CheckParams annotationCheckParams = getAnnotationCheckParams(joinpoint);
        if(null == annotationCheckParams) {
            return;
        }

        Object args = joinpoint.getArgs()[0];


        if(args instanceof com.gzsz.cs.api.base.ReqDTO) {
            com.gzsz.cs.api.base.ReqDTO reqDTO = (com.gzsz.cs.api.base.ReqDTO) args;

            System.out.println(reqDTO.toString());
            AssetCouponDTO assetCouponDTO = JacksonUtils.json2obj(reqDTO.getBizParams(), AssetCouponDTO.class);
            String uid = assetCouponDTO.getUid();
            if(null == uid || StrUtil.isBlankIfStr(uid)) {

                System.out.println( "uid不能为null,请登录");
            }

        }else {
            log.error("请求参数类型不正确,参数:{}", args.toString());
        }


    }



    /**
     * 是否存在注解,如果存在就获取
     */
    private CheckParams getAnnotationCheckParams(JoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(CheckParams.class);
        }
        return null;
    }


}

2. 异步顺序任务

上一篇 下一篇

猜你喜欢

热点阅读