在 Spring MVC 中配置默认​​的 Kotlin 协程上下文

Configure default Kotlin coroutine context in Spring MVC

我需要为 Spring MVC 中的所有请求配置默认协程上下文。例如 MDCContext(与 this 类似的问题,但对于 MVC 而不是 WebFlux)。

我试过的

  1. 挂接到 Spring - 协程代码是 here 但无法更改默认行为(需要更改 InvocableHandlerMethod.doInvoke 实现)
  2. 使用 AOP - 不使用 AOP 和协程 play well together

有什么想法吗?

这似乎有效:

@Configuration
class ContextConfig: WebMvcRegistrations {

    override fun getRequestMappingHandlerAdapter(): RequestMappingHandlerAdapter {
        return object: RequestMappingHandlerAdapter() {
            override fun createInvocableHandlerMethod(handlerMethod: HandlerMethod): ServletInvocableHandlerMethod {
                return object : ServletInvocableHandlerMethod(handlerMethod) {
                    override fun doInvoke(vararg args: Any?): Any? {
                        val method = bridgedMethod
                        ReflectionUtils.makeAccessible(method)
                        if (KotlinDetector.isSuspendingFunction(method)) {
                            // Exception handling skipped for brevity, copy it from super.doInvoke()
                            return invokeSuspendingFunctionX(method, bean, *args)
                        }
                        return super.doInvoke(*args)
                    }

                    /**
                     * Copied from CoroutinesUtils in order to be able to set CoroutineContext
                     */
                    @Suppress("UNCHECKED_CAST")
                    private fun invokeSuspendingFunctionX(method: Method, target: Any, vararg args: Any?): Publisher<*> {
                        val function = method.kotlinFunction!!
                        val mono = mono(YOUR_CONTEXT_HERE) {
                            function.callSuspend(target, *args.sliceArray(0..(args.size-2))).let { if (it == Unit) null else it }
                        }.onErrorMap(InvocationTargetException::class.java) { it.targetException }
                        return if (function.returnType.classifier == Flow::class) {
                            mono.flatMapMany { (it as Flow<Any>).asFlux() }
                        }
                        else {
                            mono
                        }
                    }
                }
            }
        }
    }
}