任何位置的参数的切入点表达式

问题描述 投票:4回答:2
@Before(value = "@annotation(OwnershipCheck) && args(enquiry)")
public void checkOwnership(Enquiry enquiry) throws Throwable
{
}

上述表达式将匹配任何带有OwnershipCheck注释的方法,并将查询作为参数。

如何使用OwnershipCheck注释为任何方法扩展此表达式,并在有或没有其他参数的任何位置进行查询。

也就是说,需要匹配

@OwnershipCheck    
public void one(Enquiry enquiry)

@OwnershipCheck
public void two(Object obj, Enquiry enquiry)

@OwnershipCheck
public void three(Enquiry enquiry, Object ob)

@OwnershipCheck
public void four(Object obj, Enquiry enquiry, Object other)
java aspectj
2个回答
1
投票

我是这样做的:

@Pointcut("@annotation(protections) && args(request,..)")
private void httpRequestAsFirstParam(Protections protections, HttpServletRequest request){}

@Pointcut("@annotation(protections) && args(..,request)")
private void httpRequestAsLastParam(Protections protections, HttpServletRequest request){}

@Pointcut("@annotation(protections) && args(*,request,..)")
private void httpRequestAsSecondParam(Protections protections, HttpServletRequest request){}

@Around("httpRequestAsFirstParam(protections, request) " +
        "|| httpRequestAsLastParam(protections, request) " +
        "|| httpRequestAsSecondParam(protections, request)")
public Object protect(ProceedingJoinPoint joinPoint, Protections protections, HttpServletRequest request) throws Throwable {
    //...
}

你不能做args(.., enquiry, ..),但有可能使用args(*, enquiry, ..)并结合第一个和最后一个参数匹配器。


0
投票

试试这个:

@Before(value = "@annotation(OwnershipCheck) && args(.., enquiry, ..)")
public void checkOwnership(Enquiry enquiry) throws Throwable
{ }

但我不确定@AspectJ是否支持这一点。所以,你可能需要这样做:

before(Enquiry enquiry) : 
    execution(@OwnershipCheck * *(.., Enquiry, ..)) && args(.., enquiry, ..) { }
© www.soinside.com 2019 - 2024. All rights reserved.