Spring AOP @Before 建议return 值

Spring AOP @Before advice return value

我是 Spring AOP 的新手,我想知道是否可以 return 来自 @Before 的值到方法并在其中使用这个变量,例如:

@Before("@annotation(CheckUserReservationPermission) && args(username,idReservation)")
public Reservation userCreationAdvice(ProceedingJoinPoint pjp, String username, Integer idReservation) throws Throwable {
    Reservation reservation = reservationServices.findById(idReservation);
    if (!reservation.getUser().getUsername().equals(username))
        throw new PermissionException("You can't delete the reservation with id: " + idReservation);
    return reservation;
}

和我的方法:

@Override
@CheckUserReservationPermission
public void deleteReservationById(String username, Integer idReservation) throws QueryException {
    synchronized(ReservationsSchedulerServicesImpl.class){
        databaseReservationServices.deleteReservationById(username, reservation);  
    }
}

有没有办法做到这一点?否则我必须重复查询。 谢谢

UPDATE:使用 @Around 我可能有这段代码,但我怎样才能将变量检索到 deleteReservationById 方法?

@Around("@annotation(CheckUserReservationPermission) && args(username,idReservation)")
public Object userCreationAdvice(ProceedingJoinPoint pjp, String username, Integer idReservation) throws Throwable {
    Reservation reservation = reservationServices.findById(idReservation);
    if (!reservation.getUser().getUsername().equals(username))
        throw new PermissionException("You can't delete the reservation with id: " + idReservation);
    return pjp.proceed(new Object[] {reservation});
}

编辑 2:

  1. 建议

    @Around("@annotation(CheckUserReservationPermission) && args(username,idReservation)")
    public Object userCreationAdvice(ProceedingJoinPoint pjp, DeleteByIdRequest req) throws Throwable {
        Reservation reservation = reservationServices.findById(idReservation);
        if (!reservation.getUser().getUsername().equals(username)) {
            throw new PermissionException("You can't delete the reservation with id: " + idReservation);}
    
         req.setReservation(reservation);  
        return pjp.proceed(new Object[] {req});
    

    }

2。 新请求 POJO

 class DeleteByIdRequest {
      Reservation reservation;
      String username;
      Integer idReservation;
    }

3.Target 方法

@Override
@CheckUserReservationPermission
public void deleteReservationById(DeleteByIdRequest request) throws QueryException {
    synchronized(ReservationsSchedulerServicesImpl.class){
        databaseReservationServices.deleteReservationById(username, reservation);  
    }
}

看到这个提示的接口,

检查他们 return.

1.ThrowsAdvice

public void afterThrowing(IllegalArgumentException e) throws Throwable {
}

2.AfterReturningAdvice

public void afterReturning(Object returnValue, Method method,
        Object[] args, Object target) throws Throwable {
}

3.MethodBeforeAdvice

public void before(Method method, Object[] args, Object target)
        throws Throwable {

}

4.MethodInterceptor(围绕建议)

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

}

如果您在第 4 点中注意到 只有 Around 建议是 returning 对象。

你必须定义 joinPoint.proceed() 来控制拦截器什么时候应该 return 控制到原始方法。

检查简单示例here

编辑: 我认为这可以在 proceeding with arguments 的帮助下实现
基本上您可以使用新参数调用 proceed()。