-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Coroutines support to Spring AOP
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-22462
- Loading branch information
Showing
11 changed files
with
278 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ingframework/aop/framework/autoproxy/AspectJAutoProxyInterceptorKotlinIntegrationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
spring-aop/src/main/java/org/springframework/aop/framework/CoroutinesUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.