使用 Spring AOP 时,在单个连接点上绑定参数的多个环绕通知会导致错误 [英] Multiple Around advices with argument binding on a single join point causes error when using Spring AOP

查看:64
本文介绍了使用 Spring AOP 时,在单个连接点上绑定参数的多个环绕通知会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个方法上写了 2 个注释和 2 个处理每个注释值的 around 建议.

I wrote 2 annotations on a method and 2 Around advices to process each annotation values.

连接点方法是这样的:

@CacheFetch(cacheName = CacheManager.CACHE_DATASOURCE_INFO)
@TenantAware(method = OperationMethod.OPERATION, operation = OperationType.GET)
public DataSourceInfo fetchDataSource(String sourceId) {...}

这样的建议 1:

@Around("within(com.xx.yy.zz..*) && @annotation(fetch)")
public Object fetchFromCache(ProceedingJoinPoint pjp, CacheFetch fetch) throws Throwable {...}

像这样的建议2:

@Around("isXXX() && @annotation(tenantAware)")
public Object handleTenantAware(ProceedingJoinPoint pjp, TenantAware tenantAware) throws Throwable {...}

这两个通知在不同的 Aspect 类中,并且 Aspect 类都实现了 Ordered 接口.当程序到达fetchDataSource连接点方法时,出现异常:

The 2 advices are in difference Aspect classes and the Aspect classes both implemented Ordered interface. When the program reaches the fetchDataSource join point method, an exception occurs:

java.lang.IllegalStateException: Required to bind 2 arguments, but only bound 1 (JoinPointMatch was NOT bound in invocation)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.argBinding(AbstractAspectJAdvice.java:591)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)

如果我删除其中一个建议,错误就会消失,而其余的建议可以正常工作.我搜索过这个问题,大部分结果来自非常古老的 spring 版本.

If I remove one of the advice, the error disappears, and the remaining advice works correctly. I've searched the issue, and most of the result were from very old spring versions.

我当前的 Spring 框架和方面版本是 4.1.6.我尝试升级到 4.1.9 和 4.3.20,问题仍然存在.

My current Spring framework and aspects version is 4.1.6. I tried upgrading to 4.1.9 and 4.3.20, and the issue persists.

恕我直言,上面的代码应该可以工作,我自己也没有发现任何错误.我不确定这是一个错误还是其他我不知道的东西.任何帮助将不胜感激.谢谢.

IMHO the codes above should work, and I didn't find any mistakes myself. I'm not sure if it is a bug or something else I don't know. Any help will be appreciated. Thank you.

推荐答案

问题已解决,但仍有问题.

Problem resolved, but there are still questions.

我使用了 2 个方面类,其顺序设置为 Ordered.HIGHEST_PRECEDENCEOrdered.LOWEST_PRECEDENCE.如果我用另一个值替换订单值 Ordered.HIGHEST_PRECEDENCE,错误就会消失.相当奇怪的现象,异常显示与实际原因无关.有人知道真正的原因吗?

I was using the 2 aspect classes with order set to Ordered.HIGHEST_PRECEDENCE and Ordered.LOWEST_PRECEDENCE. If I replace the order value Ordered.HIGHEST_PRECEDENCE with another value, the error disappears. Quite weird phenomenon, and the exception showed nothing to do with the actual cause. Does anyone knows the actual reason?

这篇关于使用 Spring AOP 时,在单个连接点上绑定参数的多个环绕通知会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆