Auto-configure rest client when virtual threads are enabled

Refine `RestClientAutoConfiguration` conditional so that it
applies in reactive web applications if virtual threads are
active and a task executor is configured.

See gh-44952

Signed-off-by: Dmitry Sulman <dmitry.sulman@gmail.com>
This commit is contained in:
Dmitry Sulman
2025-03-30 18:10:01 +03:00
committed by Phillip Webb
parent 2f30c52269
commit 3686e9a111
3 changed files with 85 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2012-2025 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.boot.autoconfigure.web.client;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.thread.Threading;
import org.springframework.context.annotation.Conditional;
/**
* {@link SpringBootCondition} that applies when running in a non-reactive web application
* or virtual threads are enabled.
*
* @author Dmitry Sulman
*/
class NotReactiveWebApplicationOrVirtualThreadsEnabledCondition extends AnyNestedCondition {
NotReactiveWebApplicationOrVirtualThreadsEnabledCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@Conditional(NotReactiveWebApplicationCondition.class)
private static final class NotReactiveWebApplication {
}
@ConditionalOnThreading(Threading.VIRTUAL)
private static final class VirtualThreadsEnabled {
}
}

View File

@@ -53,7 +53,7 @@ import org.springframework.web.client.RestClient.Builder;
@AutoConfiguration(after = { HttpClientAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
SslAutoConfiguration.class })
@ConditionalOnClass(RestClient.class)
@Conditional(NotReactiveWebApplicationCondition.class)
@Conditional(NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.class)
public class RestClientAutoConfiguration {
@Bean

View File

@@ -20,6 +20,8 @@ import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
@@ -31,6 +33,8 @@ import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Red
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.context.annotation.Bean;
@@ -260,6 +264,39 @@ class RestClientAutoConfigurationTests {
});
}
@Test
void whenReactiveWebApplicationRestClientIsNotConfigured() {
new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
.run((context) -> {
assertThat(context).doesNotHaveBean(HttpMessageConvertersRestClientCustomizer.class);
assertThat(context).doesNotHaveBean(RestClientBuilderConfigurer.class);
assertThat(context).doesNotHaveBean(RestClient.Builder.class);
});
}
@Test
void whenServletWebApplicationRestClientIsConfigured() {
new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
assertThat(context).hasSingleBean(RestClient.Builder.class);
});
}
@Test
@EnabledForJreRange(min = JRE.JAVA_21)
void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() {
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
assertThat(context).hasSingleBean(RestClient.Builder.class);
});
}
@Configuration(proxyBeanMethods = false)
static class CodecConfiguration {