spring aop 加注解

重复执行代码:

    

   1.定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Repeatable {
int value();
}

2.定义切面

/**
* @Author dengYinYue
* @Date 2023/3/28 10:17
*/
@Aspect
@Component
public class RepeatableAspect {

// 定义切点,匹配所有带有@Repeatable注解的方法
@Pointcut("@annotation(com.td.spec.muji.trade.job.executor.service.biz.impl.Repeatable)")
public void repeatableMethod() {}

// 定义通知,在方法执行前后织入逻辑
@Around("repeatableMethod() && @annotation(repeatable)")
public void repeatMethod(ProceedingJoinPoint joinPoint, Repeatable repeatable) throws Throwable {
// 获取@Repeatable注解的值
int count = repeatable.value();
// 执行方法多次
for (int i = 0; i < count; i++) {
joinPoint.proceed();
}
}
}

3.方法使用注解
@Repeatable(3)
@Repeatable(value = 1,value2 = "88")


注意:spring boot 开启aop自动配 @EnableAspectJAutoProxy

 

posted @ 2023-03-28 10:46  91程序猿  阅读(57)  评论(0编辑  收藏  举报