首页 > 解决方案 > Spring AOP 和 Reactor 中的多个有序注释出错

问题描述

我有这两个注释及其各自的方面:

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class BackupCache
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Cb(val circuitBreakerConfig: CircuitBreakerManager.CircuitBreakerConfigs) {}
@Aspect
@Component
class CircuitBreakerAnnotationAspect(meterRegistry: MeterRegistry? = null) {
    @Around("@annotation(cb)")
    fun aroundAnnotatedCBMethod(joinPoint: ProceedingJoinPoint, cb: Cb): Mono<*> {
        val signature = joinPoint.signature
        return wrap(signature, cb) {
            joinPoint.proceed() as Mono<*>
        }
    }
}

@Aspect
@Component
class BackupCacheAspect(@Autowired val backupCacheService: BackupCacheService) : Ordered {
    @Around("@annotation(backupCache)")
    fun aroundAnnotatedCBMethod(joinPoint: ProceedingJoinPoint, backupCache: BackupCache): Mono<ApiDataWrapper> {
       // ...
    }

    override fun getOrder(): Int = 1000
}

我需要它们井井有条,Cb被称为最内层和BackupCache最外层。如果我对两者都下令(使用Cb order = 10),则会出现以下错误:

java.lang.IllegalStateException: Required to bind 2 arguments, but only bound 1 (JoinPointMatch was NOT bound in invocation)

测试重现:

@Component
class Many {
    @BackupCache
    @Cb(DEFAULT)
    fun methodWithAnnotations(id: String): Mono<ApiDataWrapper> {
        return Mono.empty()
    }
}
@SpringBootTest
class MultipleAnnotationsOnServicesTests {
    @Autowired
    lateinit var many: Many

    @Test
    fun testMultipleAnnotations(){
        StepVerifier.create(many.methodWithAnnotations("aaa"))
                .verifyComplete()
    }
}

删除其中一个命令可以解决问题,但我需要执行命令。任何人都可以帮忙吗?

标签: springspring-aopspring-reactivespring-reactor

解决方案


推荐阅读