代码之家  ›  专栏  ›  技术社区  ›  Manrico Corazzi

AspectJ:切入点中的参数

  •  6
  • Manrico Corazzi  · 技术社区  · 16 年前

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

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

    public void delete(Object item, Session currentSession);
    

    但它不适用于以下方法:

    public List listAll(Session currentSession);
    

    4 回复  |  直到 4 年前
        1
  •  8
  •   Manrico Corazzi    16 年前

    哦,好吧。。。我用了这个下流的把戏。仍在等待有人给出“官方”切入点定义。

    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
  •  4
  •   Tahir Akhtar    16 年前

    怎么样:

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

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

        3
  •  3
  •   kriegaex    12 年前

    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());
        }
    }
    

    within(SomeBaseClass+) within(*Postfix) within(com.company.package..*) 这样就不会把建议应用于整个宇宙。

        4
  •  3
  •   Iomanip    8 年前

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

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

    && args(*, sess) 因为这意味着您希望只捕获第一个参数的类型为任意的方法,但是 sess 作为第二个参数,且不超过2个参数。。

        5
  •  0
  •   Chirag Thakkar    5 年前
    @Before(value = "execution(public * *(.., org.springframework.data.domain.Pageable , ..))")
    private void isMethodPageable () {
        log.info("in a Aspect point cut isPageableParameterAvailable()");
    }