@Around("pointCutIAntiResubmit(iAntiResubmit)") public Object aroundAdvice(ProceedingJoinPoint pjp, IAntiResubmit iAntiResubmit) { String key = loadKey(iAntiResubmit.key(), pjp); String redisKey = RedisConstants.ANTI_RESUBMIT_REDIS_KEY + RedisConstants.SEPARATOR + getValue(pjp, iAntiResubmit) + "::" + key; RLock lock = redissonClient.getLock(redisKey); try { Object[] args = pjp.getArgs();// 得到方法执行所需的参数 lock.lock(); return pjp.proceed(args);// 明确调用切入点方法(切入点方法) } catch (SysException se) { throw se; } catch (Throwable e) { throw new SysException(e.getMessage()); } finally { lock.unlock(); } }这里的iAntiResubmit.key()是不是为空
时间: 2024-02-14 14:33:09 浏览: 30
如果IAntiResubmit注解被正确地使用,并且在使用该注解时为key成员变量显式地赋值,那么iAntiResubmit.key()应该是非空的。
在上面的代码中,通过iAntiResubmit.key()获取IAntiResubmit注解的key成员变量的值,并将其作为loadKey方法的第一个参数。如果IAntiResubmit注解的key成员变量没有显式地赋值,那么iAntiResubmit.key()将返回空字符串,但是在loadKey方法中,空字符串会被视为无效的key值,可能会导致程序出错。
因此,在使用IAntiResubmit注解时,应该为其key成员变量显式地赋值,以确保程序能够正确地执行。
相关问题
import org.aspectj.lang.ProceedingJoinPoint.procced
ProceedingJoinPoint是Spring AOP中的一个接口,它继承了JoinPoint接口,并在其基础上暴露了proceed()方法[^1]。proceed()方法是用于启动目标方法执行的,它在环绕通知中起到关键作用。环绕通知由前置通知、目标方法执行和后置通知组成,而proceed()方法决定了是否继续执行代理链或者执行其他拦截逻辑。
在Spring AOP中,proceed()方法只在环绕通知中被支持,而其他类型的通知(如前置通知、后置通知)只需要使用JoinPoint接口即可。通过调用proceed()方法,可以决定是否继续执行代理链或者执行其他逻辑。
以下是一个使用ProceedingJoinPoint的示例代码:
```java
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 在目标方法执行之前的逻辑
System.out.println("Before advice");
// 执行目标方法
Object result = proceedingJoinPoint.proceed();
// 在目标方法执行之后的逻辑
System.out.println("After advice");
return result;
}
}
```
在上述示例中,aroundAdvice()方法是一个环绕通知,它接收一个ProceedingJoinPoint参数。在方法中,我们可以在目标方法执行之前执行一些逻辑,然后调用proceed()方法来启动目标方法的执行,最后在目标方法执行之后执行一些逻辑。
java @Around 怎么在@ControllerAdvice之后执行
在 Spring AOP 中,@ControllerAdvice 通常是用来定义全局异常处理的。而 @Around 注解则是用来定义环绕通知的,可以在切点方法执行前后执行一些自定义的操作。
如果你想要在 @ControllerAdvice 执行完之后再执行 @Around,可以在 @Around 中使用 ProceedingJoinPoint.proceed() 方法来手动执行切点方法。具体实现可以参考下面的代码示例:
```java
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
// 处理异常
}
}
@Aspect
@Component
public class AroundAdvice {
@Around("execution(* com.example.controller.*.*(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
try {
// 在 @ControllerAdvice 执行完之后再执行 @Around
result = joinPoint.proceed();
} catch (Throwable throwable) {
// 处理异常
}
return result;
}
}
```
在上面的代码中,@Around 注解的切点是 com.example.controller 包下的所有方法。当切点方法被调用时,@ControllerAdvice 会先执行异常处理,然后再执行 @Around 中定义的环绕通知。