Skip to content

Commit 7d47c9b

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

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,13 +19,17 @@
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;
2527
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
2628
import org.springframework.boot.autoconfigure.http.client.HttpClientAutoConfiguration;
2729
import org.springframework.boot.ssl.SslBundles;
2830
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
31+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
32+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2933
import org.springframework.boot.web.client.RestClientCustomizer;
3034
import org.springframework.boot.web.codec.CodecCustomizer;
3135
import org.springframework.context.annotation.Bean;
@@ -171,6 +175,39 @@ void whenHasFactoryProperty() {
171175
});
172176
}
173177

178+
@Test
179+
void whenReactiveWebApplicationRestClientIsNotConfigured() {
180+
new ReactiveWebApplicationContextRunner()
181+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
182+
.run((context) -> {
183+
assertThat(context).doesNotHaveBean(HttpMessageConvertersRestClientCustomizer.class);
184+
assertThat(context).doesNotHaveBean(RestClientBuilderConfigurer.class);
185+
assertThat(context).doesNotHaveBean(RestClient.Builder.class);
186+
});
187+
}
188+
189+
@Test
190+
void whenServletWebApplicationRestClientIsConfigured() {
191+
new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
192+
.run((context) -> {
193+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
194+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
195+
assertThat(context).hasSingleBean(RestClient.Builder.class);
196+
});
197+
}
198+
199+
@Test
200+
@EnabledForJreRange(min = JRE.JAVA_21)
201+
void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() {
202+
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
203+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
204+
.run((context) -> {
205+
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
206+
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
207+
assertThat(context).hasSingleBean(RestClient.Builder.class);
208+
});
209+
}
210+
174211
@Configuration(proxyBeanMethods = false)
175212
static class CodecConfiguration {
176213

0 commit comments

Comments
 (0)