Spring AOP-仅当从lambda调用joinPoint.proceed时,Pointcut才应用于方法



(标题不是最好的,但我找不到一个好的方法来表达以下问题(

给定

@Aspect
@Component
class MyAspect {
@Autowired private MyService service;
@Around("@target(org.springframework.ws.server.endpoint.annotation.Endpoint)")
public Object aroundEndpoint(ProceedingJoinPoint joinPoint) {
return service.around(joinPoint::proceed);
}
@Around("@target(org.springframework.stereotype.Service)") // And some other expressions to exclude `MyService`
public Object aroundService(ProceedingJoinPoint joinPoint) throws Throwable {
// ...
}
}
@Service
class MyService {
// My own Callable<T> with Throwable instead of Exception
public Object around(Callable<?> callable) throws Throwable {
// Do stuff
Object returnValue = callable.call();
// Do stuff
return returnValue;
}
}

当一个端点方法被调用时,它会被aroundEndpoint截获。如果我立即调用joinPoint.proceed(),一切都会按预期进行。但是,如果我将它作为方法引用(或lambda(传递到MyService.around中,并且然后调用它,它将与我的服务切入点相匹配,并且我的around服务建议将应用于它。

我做了一些调试,下面是我看到的:在AspectJExpressionPointcut.matches中,thisObjecttargetObject在前一种情况下指我的端点,但在后一种情况中指我的服务。这可能是因为它使用ExposeInvocationInterceptor.currentInvocation(),而进行另一个方法调用会使它出错

这是个虫子吗?基于代理的方法有什么局限性?还是我必须简单地内联MyService.aroundService

我重现了您的问题,并比较了普通Java+AspectJ中的类似设置(即,没有Spring或Spring AOP,仅使用方面切入点中使用的两个Spring注释(。在那里问题没有发生。这是Spring AOP特有的东西,这点是肯定的。

现在Spring将AspectJ的切入点匹配与它自己的基于代理和委托的AOP框架结合使用。在那里的某个地方,这种边缘情况一定会扰乱Spring方面匹配的状态,从而导致您看到的行为。到目前为止,我还没有调试到它,但从我现在看到的情况来看,我建议创建一个问题,看看维护人员对此有何看法

下面是我的AspectJ MCVE,它证明了问题不会出现在那里。顺便说一句,我不得不将包aspect重命名为aop,因为在AspectJ中aspect是一个保留关键字。但我也在Spring项目中重命名了它,以确保它与手头的问题无关,而且它是不相关的。

package aop.mcve;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
public void controllerMethod() {}
}
package aop.mcve;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public Object delegateTo(MyAspect.Callable<?> callable) throws Throwable {
return callable.call();
}
public void serviceMethod() {}
}
package aop.mcve;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
private final MyService myService = new MyService();
@Pointcut("within(aop.mcve..*) && !within(MyAspect) && execution(* *(..))")
public void inDomain() {}
@Pointcut("@target(org.springframework.stereotype.Service)")
public void inService() {}
@Pointcut("execution(* aop.mcve.MyService.*(..))")
public void inMyService() {}
@Pointcut("@target(org.springframework.web.bind.annotation.RestController)")
public void inController() {}
@Around("inDomain() && inController()")
public Object aroundController(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aroundController -> " + joinPoint);
return myService.delegateTo(joinPoint::proceed);
}
@Around("inDomain() && inService() && !inMyService()")
public Object aroundService(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aroundService -> " + joinPoint);
System.out.println("You should never see this message!");
return joinPoint.proceed();
}
public interface Callable<T> {
T call() throws Throwable;
}
}
package aop.mcve;
public class AspectMcveApplication {
public static void main(String[] args) throws Throwable {
new MyService().serviceMethod();
new MyController().controllerMethod();
}
}

控制台日志:

aroundController -> execution(void aop.mcve.MyController.controllerMethod())

正如您所看到的,建议方法aroundService(..)并不像Spring AOP中那样被触发。


更新:我修改了MCVE,使其可以与Spring AOP和AspectJ一起运行,它在活动时自动检测AspectJ的加载时间编织器。我给你发了这个提款请求。

最新更新