我正在使用 Spring AOP 并且有以下方面:
@Aspect
public 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("******");
}
}
Above aspect 拦截 addCustomer
方法的执行。 addCustomer
方法将字符串作为输入。
但我需要在 logBefore
方法中记录传递给 addCustomer
方法的输入。
有可能吗?
最佳答案
你有几个选择:
首先,您可以使用 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)
关于java - 使用 Spring AOP 获取方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660535/