Skip to content

Commit fa191d8

Browse files
committed
Align ReactiveWebApplicationContext with framework
Refactor `ReactiveWebApplicationContext` implementations to align closer with the `WebApplicationContext` implementations defined in Spring Framework. The following classes are now provided: - `AnnotationConfigReactiveWebApplicationContext` -- A refreshable reactive web context with support for `@Configuration` classes. - `GenericReactiveWebApplicationContext` -- A non-refreshable reactive GenericApplicationContext. - `ReactiveWebServerApplicationContext` -- A non-refreshable reactive GenericApplicationContext with support for server discovery. - `AnnotationConfigReactiveWebServerApplicationContext` -- A non-refreshable reactive `GenericApplicationContext` with support for `@Configuration` classes and server discovery. These classes roughly align to the following Servlet equivalents: - `AnnotationConfigWebApplicationContext` (Spring Framework) - `GenericWebApplicationContext` (Spring Framework) - `ServletWebServerApplicationContext` (Spring Boot) - `AnnotationConfigServletWebServerApplicationContext` (Spring Boot) An additional `ConfigurableReactiveWebEnvironment` interface as also been introduced, primarily for `@ConditionalOnWebApplication` to use. Fixes gh-10852
1 parent b6166dc commit fa191d8

File tree

24 files changed

+968
-111
lines changed

24 files changed

+968
-111
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import org.springframework.beans.factory.support.RootBeanDefinition;
2626
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
2727
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration;
28-
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
29-
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
28+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
3029
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
3130
import org.springframework.context.ApplicationContext;
3231
import org.springframework.context.ConfigurableApplicationContext;
@@ -41,7 +40,7 @@ class ReactiveManagementContextFactory implements ManagementContextFactory {
4140
@Override
4241
public ConfigurableApplicationContext createManagementContext(
4342
ApplicationContext parent, Class<?>... configClasses) {
44-
ReactiveWebServerApplicationContext child = new ReactiveWebServerApplicationContext();
43+
AnnotationConfigReactiveWebServerApplicationContext child = new AnnotationConfigReactiveWebServerApplicationContext();
4544
child.setParent(parent);
4645
child.register(configClasses);
4746
child.register(ReactiveWebServerAutoConfiguration.class);
@@ -50,7 +49,7 @@ public ConfigurableApplicationContext createManagementContext(
5049
}
5150

5251
private void registerReactiveWebServerFactory(ApplicationContext parent,
53-
GenericReactiveWebApplicationContext childContext) {
52+
AnnotationConfigReactiveWebServerApplicationContext childContext) {
5453
try {
5554
ConfigurableListableBeanFactory beanFactory = childContext.getBeanFactory();
5655
if (beanFactory instanceof BeanDefinitionRegistry) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.springframework.boot.actuate.endpoint.web.annotation.WebAnnotationEndpointDiscoverer;
4141
import org.springframework.boot.endpoint.web.EndpointMapping;
4242
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
43-
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
43+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
4444
import org.springframework.boot.web.reactive.context.ReactiveWebServerInitializedEvent;
4545
import org.springframework.context.ApplicationContext;
4646
import org.springframework.context.ApplicationListener;
@@ -165,21 +165,17 @@ public void linksToOtherEndpointsWithRestrictedAccess() {
165165
.doesNotExist().jsonPath("_links.test-part").doesNotExist());
166166
}
167167

168-
private ReactiveWebServerApplicationContext createApplicationContext(
168+
private AnnotationConfigReactiveWebServerApplicationContext createApplicationContext(
169169
Class<?>... config) {
170-
ReactiveWebServerApplicationContext context = new ReactiveWebServerApplicationContext();
170+
AnnotationConfigReactiveWebServerApplicationContext context = new AnnotationConfigReactiveWebServerApplicationContext();
171171
context.register(config);
172172
return context;
173173
}
174174

175-
protected int getPort(ReactiveWebServerApplicationContext context) {
176-
return context.getBean(CloudFoundryReactiveConfiguration.class).port;
177-
}
178-
179175
private void load(Class<?> configuration, Consumer<WebTestClient> clientConsumer) {
180176
BiConsumer<ApplicationContext, WebTestClient> consumer = (context,
181177
client) -> clientConsumer.accept(client);
182-
ReactiveWebServerApplicationContext context = createApplicationContext(
178+
AnnotationConfigReactiveWebServerApplicationContext context = createApplicationContext(
183179
configuration, CloudFoundryReactiveConfiguration.class);
184180
context.refresh();
185181
try {
@@ -191,6 +187,10 @@ private void load(Class<?> configuration, Consumer<WebTestClient> clientConsumer
191187
}
192188
}
193189

190+
protected int getPort(AnnotationConfigReactiveWebServerApplicationContext context) {
191+
return context.getBean(CloudFoundryReactiveConfiguration.class).port;
192+
}
193+
194194
private String mockAccessToken() {
195195
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
196196
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
3939
import org.springframework.boot.endpoint.web.EndpointMapping;
4040
import org.springframework.boot.test.util.TestPropertyValues;
41-
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
41+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
4242
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
4343
import org.springframework.context.annotation.Bean;
4444
import org.springframework.context.annotation.Configuration;
@@ -61,11 +61,11 @@
6161
*/
6262
public class ReactiveCloudFoundryActuatorAutoConfigurationTests {
6363

64-
private GenericReactiveWebApplicationContext context;
64+
private AnnotationConfigReactiveWebApplicationContext context;
6565

6666
@Before
6767
public void setup() {
68-
this.context = new GenericReactiveWebApplicationContext();
68+
this.context = new AnnotationConfigReactiveWebApplicationContext();
6969
}
7070

7171
@After

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/reactive/WebFluxEndpointIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.actuate.endpoint.web.annotation.WebAnnotationEndpointDiscoverer;
2626
import org.springframework.boot.endpoint.web.EndpointMapping;
2727
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
28+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
2829
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
2930
import org.springframework.boot.web.reactive.context.ReactiveWebServerInitializedEvent;
3031
import org.springframework.context.ApplicationContext;
@@ -80,9 +81,9 @@ public void readOperationsThatReturnAResourceSupportRangeRequests() {
8081
}
8182

8283
@Override
83-
protected ReactiveWebServerApplicationContext createApplicationContext(
84+
protected AnnotationConfigReactiveWebServerApplicationContext createApplicationContext(
8485
Class<?>... config) {
85-
ReactiveWebServerApplicationContext context = new ReactiveWebServerApplicationContext();
86+
AnnotationConfigReactiveWebServerApplicationContext context = new AnnotationConfigReactiveWebServerApplicationContext();
8687
context.register(config);
8788
return context;
8889
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/test/WebFluxEndpointsRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.boot.endpoint.web.EndpointMapping;
3535
import org.springframework.boot.web.context.WebServerInitializedEvent;
3636
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
37-
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
37+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
3838
import org.springframework.context.ApplicationContext;
3939
import org.springframework.context.ApplicationListener;
4040
import org.springframework.context.ConfigurableApplicationContext;
@@ -58,7 +58,7 @@ class WebFluxEndpointsRunner extends AbstractWebEndpointRunner {
5858
}
5959

6060
private static ConfigurableApplicationContext createContext(List<Class<?>> classes) {
61-
ReactiveWebServerApplicationContext context = new ReactiveWebServerApplicationContext();
61+
AnnotationConfigReactiveWebServerApplicationContext context = new AnnotationConfigReactiveWebServerApplicationContext();
6262
classes.add(WebFluxEndpointConfiguration.class);
6363
context.register(classes.toArray(new Class<?>[classes.size()]));
6464
context.refresh();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
22+
import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebEnvironment;
2223
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
2324
import org.springframework.context.annotation.Condition;
2425
import org.springframework.context.annotation.ConditionContext;
@@ -114,6 +115,10 @@ private ConditionOutcome isServletWebApplication(ConditionContext context) {
114115

115116
private ConditionOutcome isReactiveWebApplication(ConditionContext context) {
116117
ConditionMessage.Builder message = ConditionMessage.forCondition("");
118+
if (context.getEnvironment() instanceof ConfigurableReactiveWebEnvironment) {
119+
return ConditionOutcome
120+
.match(message.foundExactly("ConfigurableReactiveWebEnvironment"));
121+
}
117122
if (context.getResourceLoader() instanceof ReactiveWebApplicationContext) {
118123
return ConditionOutcome
119124
.match(message.foundExactly("ReactiveWebApplicationContext"));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnNotWebApplicationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import reactor.core.publisher.Mono;
2222

2323
import org.springframework.boot.autoconfigure.web.reactive.MockReactiveWebServerFactory;
24-
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
24+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
2525
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
2626
import org.springframework.context.ConfigurableApplicationContext;
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -57,18 +57,17 @@ public void testNotWebApplicationWithServletContext() {
5757
ctx.register(NotWebApplicationConfiguration.class);
5858
ctx.setServletContext(new MockServletContext());
5959
ctx.refresh();
60-
6160
this.context = ctx;
6261
assertThat(this.context.getBeansOfType(String.class)).isEmpty();
6362
}
6463

6564
@Test
6665
public void testNotWebApplicationWithReactiveContext() {
67-
GenericReactiveWebApplicationContext ctx = new GenericReactiveWebApplicationContext();
68-
ctx.register(ReactiveApplicationConfig.class,
66+
AnnotationConfigReactiveWebApplicationContext context = new AnnotationConfigReactiveWebApplicationContext();
67+
context.register(ReactiveApplicationConfig.class,
6968
NotWebApplicationConfiguration.class);
70-
ctx.refresh();
71-
this.context = ctx;
69+
context.refresh();
70+
this.context = context;
7271
assertThat(this.context.getBeansOfType(String.class)).isEmpty();
7372
}
7473

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnWebApplicationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
2424
import org.springframework.boot.autoconfigure.web.reactive.MockReactiveWebServerFactory;
25-
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
25+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
2626
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
2727
import org.springframework.context.ConfigurableApplicationContext;
2828
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -67,12 +67,12 @@ public void testWebApplicationWithServletContext() {
6767

6868
@Test
6969
public void testWebApplicationWithReactiveContext() {
70-
GenericReactiveWebApplicationContext ctx = new GenericReactiveWebApplicationContext();
71-
ctx.register(AnyWebApplicationConfiguration.class,
70+
AnnotationConfigReactiveWebApplicationContext context = new AnnotationConfigReactiveWebApplicationContext();
71+
context.register(AnyWebApplicationConfiguration.class,
7272
ServletWebApplicationConfiguration.class,
7373
ReactiveWebApplicationConfiguration.class);
74-
ctx.refresh();
75-
this.context = ctx;
74+
context.refresh();
75+
this.context = context;
7676
assertThat(this.context.getBeansOfType(String.class))
7777
.containsExactly(entry("any", "any"), entry("reactive", "reactive"));
7878
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.junit.Test;
2121

2222
import org.springframework.boot.test.util.TestPropertyValues;
23-
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
23+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
2424
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.Configuration;
@@ -38,7 +38,7 @@ public class MustacheAutoConfigurationTests {
3838

3939
private AnnotationConfigWebApplicationContext webContext;
4040

41-
private GenericReactiveWebApplicationContext reactiveWebContext;
41+
private AnnotationConfigReactiveWebApplicationContext reactiveWebContext;
4242

4343
@Test
4444
public void registerBeansForServletApp() {
@@ -102,7 +102,7 @@ private void loadWithServlet(Class<?> config) {
102102
}
103103

104104
private void loadWithReactive(Class<?> config) {
105-
this.reactiveWebContext = new GenericReactiveWebApplicationContext();
105+
this.reactiveWebContext = new AnnotationConfigReactiveWebApplicationContext();
106106
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/")
107107
.applyTo(this.reactiveWebContext);
108108
if (config != null) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/ReactiveSecurityAutoConfigurationTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import org.springframework.boot.autoconfigure.AutoConfigurations;
2323
import org.springframework.boot.autoconfigure.web.reactive.MockReactiveWebServerFactory;
24-
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25-
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
24+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
2625
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
2726
import org.springframework.context.ApplicationContext;
2827
import org.springframework.context.annotation.Bean;
@@ -47,8 +46,7 @@
4746
*/
4847
public class ReactiveSecurityAutoConfigurationTests {
4948

50-
private ApplicationContextRunner contextRunner = new ApplicationContextRunner(
51-
ReactiveWebServerApplicationContext::new);
49+
private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner();
5250

5351
@Test
5452
public void enablesWebFluxSecurity() {

0 commit comments

Comments
 (0)