【问题标题】:AspectJ: parameter in a pointcutAspectJ:切入点中的参数
【发布时间】:2008-11-05 16:21:27
【问题描述】:

我正在使用 AspectJ 来建议所有具有所选类参数的公共方法。我尝试了以下方法:

pointcut permissionCheckMethods(Session sess) : 
    (execution(public * *(.., Session)) && args(*, sess));

这对于具有至少 2 个参数的方法非常有效:

public void delete(Object item, Session currentSession);

但它不适用于以下方法:

public List listAll(Session currentSession);

如何更改我的切入点以建议两种方法执行?换句话说:我希望“..”通配符代表“零个或多个参数”,但看起来它的意思是“一个或多个”......

【问题讨论】:

    标签: aop aspectj


    【解决方案1】:

    哦,好吧...我用这个讨厌的把戏解决了这个问题。仍在等待有人提供“官方”切入点定义。

    pointcut permissionCheckMethods(EhealthSession eheSess) : 
        (execution(public * *(.., EhealthSession)) && args(*, eheSess))
        && !within(it.___.security.PermissionsCheck);
    
    pointcut permissionCheckMethods2(EhealthSession eheSess) : 
        (execution(public * *(EhealthSession)) && args(eheSess))
        && !within(it.___.security.PermissionsCheck)
        && !within(it.___.app.impl.EhealthApplicationImpl);
    
    before(EhealthSession eheSess) throws AuthorizationException : permissionCheckMethods(eheSess)
    {
        Signature sig = thisJoinPointStaticPart.getSignature(); 
        check(eheSess, sig);
    }
    
    before(EhealthSession eheSess) throws AuthorizationException : permissionCheckMethods2(eheSess)
    {
        Signature sig = thisJoinPointStaticPart.getSignature(); 
        check(eheSess, sig);
    }
    

    【讨论】:

      【解决方案2】:

      怎么样:

      pointcut permissionCheckMethods(Session sess) : 
      (execution(public * *(..)) && args(.., sess));
      

      如果最后一个(或唯一一个)参数是 Session 类型,我猜这将匹配。通过交换 args 的位置,您还可以首先匹配或仅匹配。但我不知道是否可以匹配任意位置。

      【讨论】:

      • 不可能匹配任意位置。
      【解决方案3】:

      我无法为您扩展 AspectJ 语法,但我可以提供一种解决方法。但首先让我解释一下为什么不能在切入点中使用args 定义来做你想做的事:因为如果你要在方法签名中的任何地方匹配你的EhealthSession 参数,AspectJ 应该如何处理签名的情况包含该类的多个参数? eheSess 的含义不明确。

      现在的解决方法:它可能会更慢 - 多少取决于您的环境,只需测试它 - 但您可以让切入点匹配所有潜在方法,而不管它们的参数列表如何,然后让建议找到您需要的参数检查参数列表:

      pointcut permissionCheckMethods() : execution(public * *(..));
      
      before() throws AuthorizationException : permissionCheckMethods() {
          for (Object arg : thisJoinPoint.getArgs()) {
              if (arg instanceof EhealthSession)
                  check(arg, thisJoinPointStaticPart.getSignature());
          }
      }
      

      P.S.:也许您可以通过within(SomeBaseClass+) 或within(*Postfix) 或within(com.company.package..*) 缩小焦点,以免将建议应用于整个宇宙。

      【讨论】:

        【解决方案4】:

        你必须在结尾和开头使用..(双点),如下所示:

        pointcut permissionCheckMethods(Session sess) : 
            (execution(public * *(.., Session , ..)) );
        

        还要摆脱&& args(*, sess),因为这意味着您希望只捕获那些第一个参数类型为任何类型但sess作为第二个参数且不超过2个参数的方法..

        【讨论】:

        • @Manrico Corazzi 您应该将其标记为您的问题的解决方案。这样其他人就不会从其他工作中分心
        【解决方案5】:
        @Before(value = "execution(public * *(.., org.springframework.data.domain.Pageable , ..))")
        private void isMethodPageable () {
            log.info("in a Aspect point cut isPageableParameterAvailable()");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多