使用Spring AOP时,在单个连接点上具有参数绑定的多个Around建议会导致错误

时间:2018-10-22 11:20:36

标签: java spring aop spring-aop

我在一个方法上写了2条注释,并在2条“周围”建议中处理了每个注释值。

连接点方法如下:

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

Advice2像这样:

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

这两个建议在不同的Aspect类和Aspect类中都实现了Ordered接口。当程序到达fetchDataSource连接点方法时,发生异常:

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框架和方面版本是4.1.6。我尝试升级到4.1.9和4.3.20,但问题仍然存在。

恕我直言,上面的代码应该可以工作,而我自己也没有发现任何错误。我不确定这是错误还是其他未知的信息。任何帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:0)

问题已解决,但仍然存在问题。

我正在使用2个方面类,其顺序分别设置为Ordered.HIGHEST_PRECEDENCEOrdered.LOWEST_PRECEDENCE。如果我将订单值Ordered.HIGHEST_PRECEDENCE替换为另一个值,则错误消失。很奇怪的现象,并且异常与实际原因无关。有人知道真正的原因吗?

答案 1 :(得分:0)

这是由于ExposeInvocationInterceptor不能首先加载引起的,并且将导致userAttributes不会通过 Spring Bean (对于CacheFetch)注入参数。

您还可以在以下位置找到comment

No MethodInvocation found: Check that an AOP invocation is in progress, 
and that the ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, 
note that advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor!

还有一种issue尝试通过使用PriorityOrdered来解决,但似乎仍未进行。

因此,对于您的问题,有两种解决方案可以解决此问题:

  1. 更改Ordered而不是HIGHEST_PRECEDENCE,也许更改HIGHEST_PRECEDENCE+1

  2. 通过ApplicationContext.getBean

  3. 手动注入bean

答案 2 :(得分:0)

我发现了另一种情况,可能会使Exposeinvocationinterceptor中的调用无效,导致org.springframework.aop.aspectj.aspectjexpressionpointcut ා matches (java.lang.reflect.method, java.lang.class <? >, Java. Lang.Object...)无法获得有效的方法调用。

特定情况:当存在多个方面时,当高优先级方面异步处理joinpoint.Processed()时,下一个方面将切换到新线程,因此无法获取ThreadLocal变量。 enter image description here