我是新Spring AOP實現,我想知道是否有可能從@Before
返回一個值的方法和使用裏面這個變量,例如:Spring AOP的@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});
}
我們可以使用around建議嗎? – VedX
不,你不能。你只能從around建議中返回一個值。 –
並與周圍的建議我可以使用該方法的變量? – luca