【问题标题】:How to get method argument value in joinpoint.getArgs()?如何在 joinpoint.getArgs() 中获取方法参数值?
【发布时间】:2017-10-31 05:49:27
【问题描述】:
    String type = "";
if("searchClientContactDetails".equalsIgnoreCase(methodName) || "getClientAndVendorOrgDeatilsById".equalsIgnoreCase(methodName) 
                || "saveVenodrContact".equalsIgnoreCase(methodName) || "getSpocAndOwnerDetailsById".equalsIgnoreCase(methodName)
                || "terminateSpoc".equalsIgnoreCase(methodName)){
    Object[] args = joinPoint.getArgs();
            Object arg=args[0];
            Class c=arg.getClass();
            type=(String)c.getMethod("getResponderType").invoke(arg);
}

从上面的代码中,如果我的 getResponderType 值在 args[0] 中,那么我得到了所需的值,如果我的值出现在 args[1] 或 args[2] 中怎么办(对多种方法使用相同) .在我的代码中,我将在少数方法的第一个参数中获得“getResponderType”值,而在另一种方法中,我将在第二个或第三个参数中获得它。

【问题讨论】:

    标签: spring aop spring-aop


    【解决方案1】:

    如果你提供方法声明会更容易。 因为对于

     void searchClientContactDetails(String a, Integer b);      
     void searchClientContactDetails(Long a, String b);
    

    你的

    Object[] args = joinPoint.getArgs();
    Object arg=args[0];
    

    将返回不同的值。

    也许是你的问题)

    【讨论】:

      【解决方案2】:

      您可以遍历 args 以查找方法 getResponderType。一旦你找到你要找的东西,你就可以打破了。

      String type = "";
      Object[] args = joinPoint.getArgs();
      for (Object arg : args) {
          Class clazz = arg.getClass();
          try {
              Method mthd = arg.getClass().getMethod("getResponderType");
              if (mthd != null) {
                  type = (String) clazz.getMethod(
                                  "getResponderType").invoke(arg);
                  break;
              }
          } catch (Exception e) {
              //do nothing
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-30
        • 2010-10-04
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 2012-08-26
        相关资源
        最近更新 更多