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

时间:2011-04-06 15:06:14

标签: java aspectj

@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)

2 个答案:

答案 0 :(得分:1)

Here is how I did this:

@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 {
    //...
}

You can't do args(.., enquiry, ..), but it is possible with args(*, enquiry, ..) and combine with first and last argument matchers.

答案 1 :(得分:0)

试试这个:

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

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

before(Enquiry enquiry) : 
    execution(@OwnershipCheck * *(.., Enquiry, ..)) && args(.., enquiry, ..) { }