Skip to content

Commit c858a58

Browse files
committed
Add NotReactiveWebApplicationOrVirtualThreadsEnabledCondition
See #44912 Signed-off-by: Dmitry Sulman <[email protected]>
1 parent 62b88dc commit c858a58

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

+1-1
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

+37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.List;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.condition.EnabledForJreRange;
23+
import org.junit.jupiter.api.condition.JRE;
2224

2325
import org.springframework.boot.autoconfigure.AutoConfigurations;
2426
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
@@ -29,6 +31,8 @@
2931
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
3032
import org.springframework.boot.ssl.SslBundles;
3133
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
34+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
35+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3236
import org.springframework.boot.web.client.RestClientCustomizer;
3337
import org.springframework.boot.web.codec.CodecCustomizer;
3438
import org.springframework.context.annotation.Bean;
@@ -225,6 +229,39 @@ void shouldSupplyRestClientBuilderConfigurerWithAutoConfiguredHttpSettings() {
225229
});
226230
}
227231

232+
@Test
233+
void whenReactiveWebApplicationRestClientIsNotConfigured() {
234+
new ReactiveWebApplicationContextRunner()
235+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
236+
.run((context) -> {
237+
assertThat(context).doesNotHaveBean(HttpMessageConvertersRestClientCustomizer.class);
238+
assertThat(context).doesNotHaveBean(RestClientBuilderConfigurer.class);
239+
assertThat(context).doesNotHaveBean(RestClient.Builder.class);
240+
});
241+
}
242+
243+
@Test
244+
void whenServletWebApplicationRestClientIsConfigured() {
245+
new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
246+
.run((context) -> {
247+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
248+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
249+
assertThat(context).hasSingleBean(RestClient.Builder.class);
250+
});
251+
}
252+
253+
@Test
254+
@EnabledForJreRange(min = JRE.JAVA_21)
255+
void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() {
256+
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
257+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
258+
.run((context) -> {
259+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
260+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
261+
assertThat(context).hasSingleBean(RestClient.Builder.class);
262+
});
263+
}
264+
228265
@Configuration(proxyBeanMethods = false)
229266
static class CodecConfiguration {
230267

0 commit comments

Comments
 (0)