Support for async/reactive close methods (e.g. R2DBC)

Closes gh-26991
This commit is contained in:
Juergen Hoeller
2023-06-02 20:40:55 +02:00
parent 2685a35c3a
commit 322cbca0dc
4 changed files with 230 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
@@ -17,8 +17,10 @@
package org.springframework.context.annotation;
import java.io.Closeable;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ConfigurableApplicationContext;
@@ -47,6 +49,8 @@ public class DestroyMethodInferenceTests {
WithInheritedCloseMethod c8 = ctx.getBean("c8", WithInheritedCloseMethod.class);
WithDisposableBean c9 = ctx.getBean("c9", WithDisposableBean.class);
WithAutoCloseable c10 = ctx.getBean("c10", WithAutoCloseable.class);
WithCompletableFutureMethod c11 = ctx.getBean("c11", WithCompletableFutureMethod.class);
WithReactorMonoMethod c12 = ctx.getBean("c12", WithReactorMonoMethod.class);
assertThat(c0.closed).as("c0").isFalse();
assertThat(c1.closed).as("c1").isFalse();
@@ -59,6 +63,8 @@ public class DestroyMethodInferenceTests {
assertThat(c8.closed).as("c8").isFalse();
assertThat(c9.closed).as("c9").isFalse();
assertThat(c10.closed).as("c10").isFalse();
assertThat(c11.closed).as("c11").isFalse();
assertThat(c12.closed).as("c12").isFalse();
ctx.close();
assertThat(c0.closed).as("c0").isTrue();
@@ -72,6 +78,8 @@ public class DestroyMethodInferenceTests {
assertThat(c8.closed).as("c8").isFalse();
assertThat(c9.closed).as("c9").isTrue();
assertThat(c10.closed).as("c10").isTrue();
assertThat(c11.closed).as("c11").isTrue();
assertThat(c12.closed).as("c12").isTrue();
}
@Test
@@ -171,6 +179,16 @@ public class DestroyMethodInferenceTests {
public WithAutoCloseable c10() {
return new WithAutoCloseable();
}
@Bean
public WithCompletableFutureMethod c11() {
return new WithCompletableFutureMethod();
}
@Bean
public WithReactorMonoMethod c12() {
return new WithReactorMonoMethod();
}
}
@@ -242,4 +260,38 @@ public class DestroyMethodInferenceTests {
}
}
static class WithCompletableFutureMethod {
boolean closed = false;
public CompletableFuture<Void> close() {
return CompletableFuture.runAsync(() -> {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
closed = true;
});
}
}
static class WithReactorMonoMethod {
boolean closed = false;
public Mono<Void> close() {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return Mono.fromRunnable(() -> closed = true);
}
}
}