2017-09-07 599 views
0

我是新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}); 
} 
+0

我們可以使用around建議嗎? – VedX

+0

不,你不能。你只能從around建議中返回一個值。 –

+0

並與周圍的建議我可以使用該方法的變量? – luca

回答

0

編輯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); 
    } 
} 

看到這個建議的接口,

檢查返回什麼內容。

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點只圍繞建議是返回對象。

您必須定義joinPoint.proceed()來控制攔截器何時應該將控件返回到原始方法。

檢查簡單的例子here

編輯: 我認爲這是可以用的proceeding with arguments
幫助實現基本上你可以調用proceed()方法與新的論據。

+0

我在找'@ Around',但是我怎樣才能把'reservation'發送給我的方法? – luca

+0

你的意思是,如何在你的deleteReservationById()方法中處理這個對象? – VedX

+0

請檢查編輯讓我知道是否有任何混淆 – VedX