Browse Source
This commit adds support for Kotlin Coroutines to Spring AOP by leveraging CoroutinesUtils#invokeSuspendingFunction in AopUtils#invokeJoinpointUsingReflection to convert it to the equivalent Publisher return value, like in other parts of Spring Framework. That allows method interceptors with Reactive support to process related return values. CglibAopProxy#processReturnType and JdkDynamicAopProxy#invoke take care of the conversion from the Publisher return value to Kotlin Coroutines. Reactive transactional and HTTP service interface support have been refined to leverage those new generic capabilities. Closes gh-22462pull/31115/head
Sébastien Deleuze
1 year ago
11 changed files with 278 additions and 51 deletions
@ -0,0 +1,107 @@
@@ -0,0 +1,107 @@
|
||||
package org.springframework.aop.framework.autoproxy |
||||
|
||||
import kotlinx.coroutines.delay |
||||
import kotlinx.coroutines.runBlocking |
||||
import org.aopalliance.intercept.MethodInterceptor |
||||
import org.aopalliance.intercept.MethodInvocation |
||||
import org.assertj.core.api.Assertions.assertThat |
||||
import org.junit.jupiter.api.Test |
||||
import org.springframework.aop.framework.autoproxy.AspectJAutoProxyInterceptorKotlinIntegrationTests.InterceptorConfig |
||||
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor |
||||
import org.springframework.beans.factory.annotation.Autowired |
||||
import org.springframework.context.annotation.Bean |
||||
import org.springframework.context.annotation.Configuration |
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy |
||||
import org.springframework.test.annotation.DirtiesContext |
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
||||
import reactor.core.publisher.Mono |
||||
import java.lang.reflect.Method |
||||
|
||||
|
||||
/** |
||||
* Integration tests for interceptors with Kotlin (with and without Coroutines) configured |
||||
* via AspectJ auto-proxy support. |
||||
*/ |
||||
@SpringJUnitConfig(InterceptorConfig::class) |
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) |
||||
class AspectJAutoProxyInterceptorKotlinIntegrationTests( |
||||
@Autowired val echo: Echo, |
||||
@Autowired val firstAdvisor: TestPointcutAdvisor, |
||||
@Autowired val secondAdvisor: TestPointcutAdvisor) { |
||||
|
||||
@Test |
||||
fun `Multiple interceptors with regular function`() { |
||||
assertThat(firstAdvisor.interceptor.invocations).isEmpty() |
||||
assertThat(secondAdvisor.interceptor.invocations).isEmpty() |
||||
val value = "Hello!" |
||||
assertThat(echo.echo(value)).isEqualTo(value) |
||||
assertThat(firstAdvisor.interceptor.invocations).singleElement().matches { String::class.java.isAssignableFrom(it) } |
||||
assertThat(secondAdvisor.interceptor.invocations).singleElement().matches { String::class.java.isAssignableFrom(it) } |
||||
} |
||||
|
||||
@Test |
||||
fun `Multiple interceptors with suspending function`() { |
||||
assertThat(firstAdvisor.interceptor.invocations).isEmpty() |
||||
assertThat(secondAdvisor.interceptor.invocations).isEmpty() |
||||
val value = "Hello!" |
||||
runBlocking { |
||||
assertThat(echo.suspendingEcho(value)).isEqualTo(value) |
||||
} |
||||
assertThat(firstAdvisor.interceptor.invocations).singleElement().matches { Mono::class.java.isAssignableFrom(it) } |
||||
assertThat(secondAdvisor.interceptor.invocations).singleElement().matches { Mono::class.java.isAssignableFrom(it) } |
||||
} |
||||
|
||||
@Configuration |
||||
@EnableAspectJAutoProxy |
||||
open class InterceptorConfig { |
||||
|
||||
@Bean |
||||
open fun firstAdvisor() = TestPointcutAdvisor().apply { order = 0 } |
||||
|
||||
@Bean |
||||
open fun secondAdvisor() = TestPointcutAdvisor().apply { order = 1 } |
||||
|
||||
|
||||
@Bean |
||||
open fun echo(): Echo { |
||||
return Echo() |
||||
} |
||||
} |
||||
|
||||
class TestMethodInterceptor: MethodInterceptor { |
||||
|
||||
var invocations: MutableList<Class<*>> = mutableListOf() |
||||
|
||||
@Suppress("RedundantNullableReturnType") |
||||
override fun invoke(invocation: MethodInvocation): Any? { |
||||
val result = invocation.proceed() |
||||
invocations.add(result!!.javaClass) |
||||
return result |
||||
} |
||||
|
||||
} |
||||
|
||||
class TestPointcutAdvisor : StaticMethodMatcherPointcutAdvisor(TestMethodInterceptor()) { |
||||
|
||||
val interceptor: TestMethodInterceptor |
||||
get() = advice as TestMethodInterceptor |
||||
|
||||
override fun matches(method: Method, targetClass: Class<*>): Boolean { |
||||
return targetClass == Echo::class.java && method.name.lowercase().endsWith("echo") |
||||
} |
||||
} |
||||
|
||||
open class Echo { |
||||
|
||||
open fun echo(value: String): String { |
||||
return value; |
||||
} |
||||
|
||||
open suspend fun suspendingEcho(value: String): String { |
||||
delay(1) |
||||
return value; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,14 +1,18 @@
@@ -1,14 +1,18 @@
|
||||
description = "Spring AOP" |
||||
|
||||
apply plugin: "kotlin" |
||||
|
||||
dependencies { |
||||
api(project(":spring-beans")) |
||||
api(project(":spring-core")) |
||||
optional("org.apache.commons:commons-pool2") |
||||
optional("org.aspectj:aspectjweaver") |
||||
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") |
||||
testFixturesImplementation(testFixtures(project(":spring-beans"))) |
||||
testFixturesImplementation(testFixtures(project(":spring-core"))) |
||||
testFixturesImplementation("com.google.code.findbugs:jsr305") |
||||
testImplementation(project(":spring-core-test")) |
||||
testImplementation(testFixtures(project(":spring-beans"))) |
||||
testImplementation(testFixtures(project(":spring-core"))) |
||||
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") |
||||
} |
||||
|
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.framework; |
||||
|
||||
import kotlin.coroutines.Continuation; |
||||
import kotlinx.coroutines.reactive.ReactiveFlowKt; |
||||
import kotlinx.coroutines.reactor.MonoKt; |
||||
import org.reactivestreams.Publisher; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
|
||||
/** |
||||
* Package-visible class designed to avoid a hard dependency on Kotlin and Coroutines dependency at runtime. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 6.1.0 |
||||
*/ |
||||
abstract class CoroutinesUtils { |
||||
|
||||
static Object asFlow(Object publisher) { |
||||
return ReactiveFlowKt.asFlow((Publisher<?>) publisher); |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
@Nullable |
||||
static Object awaitSingleOrNull(Object mono, Object continuation) { |
||||
return MonoKt.awaitSingleOrNull((Mono<?>) mono, (Continuation<Object>) continuation); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.support |
||||
|
||||
import kotlinx.coroutines.CoroutineName |
||||
import kotlinx.coroutines.delay |
||||
import org.assertj.core.api.Assertions.assertThat |
||||
import org.junit.jupiter.api.Test |
||||
import org.springframework.util.ReflectionUtils |
||||
import reactor.core.publisher.Mono |
||||
import kotlin.coroutines.Continuation |
||||
|
||||
/** |
||||
* Tests for Kotlin support in [AopUtils]. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
class AopUtilsKotlinTests { |
||||
|
||||
@Test |
||||
fun `Invoking suspending function should return Mono`() { |
||||
val value = "foo" |
||||
val method = ReflectionUtils.findMethod(AopUtilsKotlinTests::class.java, "suspendingFunction", |
||||
String::class.java, Continuation::class.java)!! |
||||
val continuation = Continuation<Any>(CoroutineName("test")) { } |
||||
val result = AopUtils.invokeJoinpointUsingReflection(this, method, arrayOf(value, continuation)) |
||||
assertThat(result).isInstanceOfSatisfying(Mono::class.java) { |
||||
assertThat(it.block()).isEqualTo(value) |
||||
} |
||||
} |
||||
|
||||
@Suppress("unused") |
||||
suspend fun suspendingFunction(value: String): String { |
||||
delay(1) |
||||
return value; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue