springboot 自定义注解和切面不生效

controller

@RestController
@RequestMapping("app/111")
public class test {

    /**
     * 登录界面开关
     * @return
     */
    @PostMapping("/loginOnOff1")
    @CheckForm(delaySeconds = 1,checkType = "sd")
    private ApiResult loginOnOff1() {
        System.out.println("测试");
        return null;

    }
}

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckForm {

    int delaySeconds() default 5;

    String checkType() default "";
}

切面

@Aspect
@Component
@Slf4j
public class SubmitAspect {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommonCacheUtil cacheUtil;


    @Pointcut(value = "@annotation(com.fengou.common.annotation.CheckForm)")
    private void pointcut() {
        System.out.println("111111");
    }


    @Around("pointcut()&&@annotation(checkForm)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint,CheckForm checkForm ) throws Throwable {

        Integer delaySeconds = checkForm.delaySeconds();

        String checkType = checkForm.checkType();
        UserElement ue;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String token = request.getHeader(Constants.REQUEST_TOKEN_KEY);

        Boolean falg = redisTemplate.opsForValue().setIfAbsent(token+":"+checkType,1,delaySeconds, TimeUnit.SECONDS);
      //  boolean falg = true;
        if (falg){
            log.error("正确执行了");
            return proceedingJoinPoint.proceed();

        }else {
            log.error("错误执行了");
            return ApiResult.createByInfo(ResponseCode.SUBMIT_ERROR.getCode());
        }



    }
}

 

aop的原理是动态代理,被拦截的方法不能是private,将方法loginOnOff1的private改为public就可以被拦截了。修改后的代码如下:

@RestController
@RequestMapping("app/111")
public class test {

    /**

     * 登录界面开关

     * @return

     */
    @PostMapping("/loginOnOff1")
    @CheckForm(delaySeconds = 1,checkType = "sd")
    public ApiResult loginOnOff1() {
        System.out.println("测试");
        return null;
    }

}

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632