Aspect 加载 自定义注解

2017-11-14  本文已影响0人  探戏者

自定义注解类

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodCallConstraint {
    //处理器校验必须有值
    String redisKey() default "";
    int countLimit() default 60;
    long time() default 1000;
}

定义Aspect处理类

<aop:aspectj-autoproxy proxy-target-class="true"/>
@Component
@Aspect
public class MethodCallConstraintHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(MethodCallConstraintHandler.class);

    /**
     * 定义一个公共的切点
     */
    @Pointcut("@annotation(com.api.web.annotations.MethodCallConstraint)")
    public void MethodCallConstraintPointcut(){
    }

    @Before("MethodCallConstraintPointcut()")
    public void beforeMethod(JoinPoint point){
        Object[] args = point.getArgs();
        LOGGER.info("args :"+ Arrays.toString(args));
        MethodSignature signature = (MethodSignature) point.getSignature();
        LOGGER.info("目标方法为:"+signature.getDeclaringTypeName()+"."+signature.getName());
        Method method = signature.getMethod();
        MethodCallConstraint annotation = method.getAnnotation(MethodCallConstraint.class);
        LOGGER.info("目标注解:"+annotation.redisKey()+","+annotation.countLimit()+","+annotation.time());
    }
// 逻辑代码 ...
}
 @MethodCallConstraint(redisKey = "redisValue",countLimit=88,time=666)
    public String product(HttpServletRequest request) throws IOException {
//处理逻辑
}
2017-11-14 17:19:33,282 INFO  c.l.j.c.r.w.a.MethodCallConstraintHandler - [args :[org.apache.catalina.connector.RequestFacade@43fbcc32]]
2017-11-14 17:19:34,901 INFO  c.l.j.c.r.w.a.MethodCallConstraintHandler - [目标方法为:com.le.jr.cash.restapi.web.controller.CashUserController.product]
2017-11-14 17:19:50,730 INFO  c.l.j.c.r.w.a.MethodCallConstraintHandler - [目标注解:redisValue,88,666]
上一篇下一篇

猜你喜欢

热点阅读