【问题标题】:Spring AOP change value of methods argument on around adviceSpring AOP 更改围绕建议的方法参数的值
【发布时间】:2015-09-03 07:39:40
【问题描述】:

是否可以在使用 Spring AOP 执行之前根据一些检查来更改方法参数值

我的方法

public String doSomething(final String someText, final boolean doTask) {
    // Some Content
    return "Some Text";
}

建议方法

public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
    String methodName = methodInvocation.getMethod().getName();

    Object[] arguments = methodInvocation.getArguments();
    if (arguments.length >= 2) {
        if (arguments[0] instanceof String) {
            String content = (String) arguments[0];
            if(content.equalsIgnoreCase("A")) {
                // Set my second argument as false
            } else {
                // Set my second argument as true
            }
        }
    }
    return methodInvocation.proceed();
}

请建议我设置方法参数值的方法,因为该参数没有设置器选项。

【问题讨论】:

    标签: java spring spring-boot spring-aop


    【解决方案1】:

    是的,这是可能的。你需要ProceedingJoinPoint 而不是:

    methodInvocation.proceed();
    

    然后您可以使用新参数调用继续,例如:

    methodInvocation.proceed(new Object[] {content, false});
    

    http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-advice-proceeding-with-the-call

    【讨论】:

    • 请注意:重构过程中很容易跳过这些内容。如果您没有很好的测试套件,那可能会受到伤害。
    • 是的,绝对!参数必须匹配,重构工具对这种情况没有帮助。
    • 但是我使用了 MethodInterceptor 接口。我必须为 ProceedingJoinPoint 做出哪些改变
    • 你有没有试过把界面改成ProceedingJoinPoint
    【解决方案2】:

    我使用MethodInvocation得到了答案

    public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
        String methodName = methodInvocation.getMethod().getName();
    
        Object[] arguments = methodInvocation.getArguments();
        if (arguments.length >= 2) {
            if (arguments[0] instanceof String) {
                String content = (String) arguments[0];
                if(content.equalsIgnoreCase("A")) {
                    if (methodInvocation instanceof ReflectiveMethodInvocation) {
                        ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation;
                        arguments[1] = false;
                        invocation.setArguments(arguments);
                    }
                } else {
                    if (methodInvocation instanceof ReflectiveMethodInvocation) {
                        ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation;
                        arguments[1] = true;
                        invocation.setArguments(arguments);
                    }
                }
            }
        }
        return methodInvocation.proceed();
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 Spring AOP,使用@Around 创建切入点。 然后您可以使用以下代码根据条件更改方法的参数。

      int index = 0;
      Object[] modifiedArgs = proceedingJoinPoint.getArgs();
      
      for (Object arg : proceedingJoinPoint.getArgs()) {
          if (arg instanceof User) {    // Check on what basis argument have to be modified.
              modifiedArgs[index]=user;
             }
          index++;
      }
      return proceedingJoinPoint.proceed(modifiedArgs);  //Continue with the method with modified arguments.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多