Skip to content

Commit 3686e9a

Browse files
dmitrysulmanphilwebb
authored andcommitted
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 <[email protected]>
1 parent 2f30c52 commit 3686e9a

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.client;
18+
19+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
21+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
22+
import org.springframework.boot.autoconfigure.thread.Threading;
23+
import org.springframework.context.annotation.Conditional;
24+
25+
/**
26+
* {@link SpringBootCondition} that applies when running in a non-reactive web application
27+
* or virtual threads are enabled.
28+
*
29+
* @author Dmitry Sulman
30+
*/
31+
class NotReactiveWebApplicationOrVirtualThreadsEnabledCondition extends AnyNestedCondition {
32+
33+
NotReactiveWebApplicationOrVirtualThreadsEnabledCondition() {
34+
super(ConfigurationPhase.PARSE_CONFIGURATION);
35+
}
36+
37+
@Conditional(NotReactiveWebApplicationCondition.class)
38+
private static final class NotReactiveWebApplication {
39+
40+
}
41+
42+
@ConditionalOnThreading(Threading.VIRTUAL)
43+
private static final class VirtualThreadsEnabled {
44+
45+
}
46+
47+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
@AutoConfiguration(after = { HttpClientAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
5454
SslAutoConfiguration.class })
5555
@ConditionalOnClass(RestClient.class)
56-
@Conditional(NotReactiveWebApplicationCondition.class)
56+
@Conditional(NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.class)
5757
public class RestClientAutoConfiguration {
5858

5959
@Bean

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121

2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.condition.EnabledForJreRange;
24+
import org.junit.jupiter.api.condition.JRE;
2325

2426
import org.springframework.boot.autoconfigure.AutoConfigurations;
2527
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
@@ -31,6 +33,8 @@
3133
import org.springframework.boot.ssl.SslBundle;
3234
import org.springframework.boot.ssl.SslBundles;
3335
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
36+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
37+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3438
import org.springframework.boot.web.client.RestClientCustomizer;
3539
import org.springframework.boot.web.codec.CodecCustomizer;
3640
import org.springframework.context.annotation.Bean;
@@ -260,6 +264,39 @@ void shouldSupplyRestClientBuilderConfigurerWithAutoConfiguredHttpSettings() {
260264
});
261265
}
262266

267+
@Test
268+
void whenReactiveWebApplicationRestClientIsNotConfigured() {
269+
new ReactiveWebApplicationContextRunner()
270+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
271+
.run((context) -> {
272+
assertThat(context).doesNotHaveBean(HttpMessageConvertersRestClientCustomizer.class);
273+
assertThat(context).doesNotHaveBean(RestClientBuilderConfigurer.class);
274+
assertThat(context).doesNotHaveBean(RestClient.Builder.class);
275+
});
276+
}
277+
278+
@Test
279+
void whenServletWebApplicationRestClientIsConfigured() {
280+
new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
281+
.run((context) -> {
282+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
283+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
284+
assertThat(context).hasSingleBean(RestClient.Builder.class);
285+
});
286+
}
287+
288+
@Test
289+
@EnabledForJreRange(min = JRE.JAVA_21)
290+
void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() {
291+
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
292+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
293+
.run((context) -> {
294+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
295+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
296+
assertThat(context).hasSingleBean(RestClient.Builder.class);
297+
});
298+
}
299+
263300
@Configuration(proxyBeanMethods = false)
264301
static class CodecConfiguration {
265302

0 commit comments

Comments
 (0)