使用Spring AOP获取方法参数?
我正在使用Spring AOP并具有以下方面:
@Aspectpublic class LoggingAspect {
@Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
以上方面拦截了addCustomer方法的执行。addCustomer方法将字符串作为输入。但是我需要在addCustomer方法内部记录传递给方法的输入logBefore。
有可能这样做吗?
回答:
首先,你可以使用JoinPoint#getArgs()返回的方法,其中Object[]包含建议方法的所有参数。你可能需要进行一些转换,具体取决于你想对它们进行什么处理。
其次,可以args像这样使用切入点表达式:
// use '..' in the args expression if you have zero or more parameters at that point@Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..)) && args(yourString,..)")
那么你的方法可以改为
public void logBefore(JoinPoint joinPoint, String yourString) 以上是 使用Spring AOP获取方法参数? 的全部内容, 来源链接: utcz.com/qa/423705.html
