2016-07-22 504 views
0

我的ServiceImpl類包含多個方法。我有一個LoggingAspect,我想動態地將方法參數傳遞給基於ServiceImpl中調用的方法的建議。我如何實現這一目標?在下面的代碼中,我的方法有4個參數,但其中一個方法有5個參數,第三個參數有3個。那麼,如何根據方法參數的數量動態傳遞參數?Spring AOP: - 將方法參數動態地傳遞給建議

@Aspect 
public class LoggingAspect { 

@Before("allGenericAppServiceImplMethods(userid,galleryId,sid,imageName)") 
public void LoggingAdvice(JoinPoint joinPoint,String userid,String  galleryId,String sid,String imageName){ 
System.out.println("String values are "+userid+" "+sid+" "+galleryId+" "+imageName); 
} 

@Pointcut("execution(public * com.nrollup.service.impl.GenericAppServiceImpl.*(..)) && args(userid,galleryId,sid,imageName)") 
public void allGenericAppServiceImplMethods(String userid,String galleryId,String sid,String imageName) 
{ 
} 

} 

回答

0

您可以使用around建議。代碼片段供您參考:

public void myAdvice(final ProceedingJoinPoint joinPoint) throws Throwable 
{ 
    Object[] arguments = joinPoint.getArgs(); 
} 

一旦您擁有了參數數組,您可以根據需要對其進行操作。

您可以使用joinPoint.proceed()繼續進行函數調用。

+0

非常感謝好友! –

+0

@FormParam(「userid」)字符串tuserid,---->是否有可能隨着值動態地獲取參數的名稱。由於getArgs()只給出了我的值,而不是在這種情況下的名稱「userid」......即時通訊在這裏使用網絡服務... –

+0

如果您使用的是Web服務或簡單的Java程序,無所謂。你可以在我猜測的任何東西上使用AOP。 –