Get method arguments using Spring AOP?

后端 未结 8 2003
陌清茗
陌清茗 2020-11-29 01:13

I am using Spring AOP and have below aspect:

@Aspect
public class LoggingAspect {

    @Before(\"execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..         


        
8条回答
  •  佛祖请我去吃肉
    2020-11-29 01:43

    Your can use either of the following methods.

    @Before("execution(* ong.customer.bo.CustomerBo.addCustomer(String))")
    public void logBefore1(JoinPoint joinPoint) {
        System.out.println(joinPoint.getArgs()[0]);
     }
    

    or

    @Before("execution(* ong.customer.bo.CustomerBo.addCustomer(String)), && args(inputString)")
    public void logBefore2(JoinPoint joinPoint, String inputString) {
        System.out.println(inputString);
     }
    

    joinpoint.getArgs() returns object array. Since, input is single string, only one object is returned.

    In the second approach, the name should be same in expression and input parameter in the advice method i.e. args(inputString) and public void logBefore2(JoinPoint joinPoint, String inputString)

    Here, addCustomer(String) indicates the method with one String input parameter.

提交回复
热议问题