Spring 带有@annotation(...) 的AOP 在某些情况下似乎不起作用

Spring AOP with @annotation(...) seems not to work in some cases

我是 Spring AOP 的新手。我实现了一个方面,该方面在我的一种方法上运行良好。但是当我重构方法并提取一些逻辑(包括我用来触发方面的注释)时,不再调用方面。下面提供了更多详细信息。

我有类似的东西:

@RequiresCheck
public ServiceResult<AccountDto> save(AccountDto accountDto) {
    // some logic here
    accountRepository.save(account.toAccount());
    // some logic there
    return ServiceResult.ok(accountDto);
}

还有一个与此相似的方面

@Around("@annotation(requiresCheck)")
public Object checkFullSemRights(ProceedingJoinPoint joinPoint) throws Throwable {
    if (check()) {
        return joinPoint.proceed();
    }
    throw new Exception();
}

这段代码工作得很好!当我将第一个方法重构为如下所示时:

public ServiceResult<AccountDto> save(AccountDto accountDto) {
    // some logic here
    return save2(accountDto.toAccount());
}

@RequiresCheck
public ServiceResult<AccountDto> save2(Account account) {
    accountRepository.save(account);
    // some logic there
    return ServiceResult.ok(account.toAccountDto());
}

那么切面就不再执行了。我认为应该为任何具有注释“@ResuiresCheck”的方法执行方面,但这似乎不是真的。还是我遗漏了什么?

无法使用 Spring AOP 拦截内部调用。

相关信息来自documentation

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are, by definition, not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy are intercepted (and even package-visible methods, if necessary). However, common interactions through proxies should always be designed through public signatures.