-
先定义一个注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyLock { String name(); long waitTime() default 0; long leaseTime() default -1; TimeUnit timeUnit() default TimeUnit.SECONDS; }
-
再写一个切面
@Slf4j @Aspect @Component public class MyLockAspect { @Around("@annotation(lock)") public Object lock(ProceedingJoinPoint pjp, MyLock lock) throws Throwable{ log.info(lock.name()); return pjp.proceed(); } }
-
然后在要被增强的方法上加上注解
@MyLock(name="test-redison-lock", leaseTime = 10,timeUnit = TimeUnit.SECONDS)