2016-07-22 41 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。 –