java - 任意位置参数的切入点表达式

标签 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)

最佳答案

这是我的做法:

@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, ..) 并结合第一个和最后一个参数匹配器。

关于java - 任意位置参数的切入点表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568617/

相关文章:

java - Flink 键控流键为空

java - 子选择查询中的 HQL 限制

java - 注释字段x时,在构建时将注释添加到Java getter getX()

java - 注释字段的拦截分配的切入点

java - 有人可以解释一下安装的 AspectJ 框架的 bin 目录中 aj5 和 aj 脚本的用途吗

java - 如何在 Firestore 中批量写入后获取更新的文档引用?

java - 如何使用 fragment 将标题放入 ViewPager?

java - 为什么抛出异常时我的方面代码不运行?

java - AspectJ 类型模式适用于所有具有属性的方法的类型?

java - Java 中使用零填充的三重 DES 加密/解密