Manage EmbeddedWebServer in ReactiveWebApplicationContext

This commit adds an `EmbeddedWebServer` instance to the
`ReactiveWebApplicationContext` and ties it to the application
lifecycle.

To launch a reactive web application, two elements are required
from the context:

* a `ReactiveWebServerFactory` to create a server instance
* a `HttpHandler` instance to handle HTTP requests

Closes gh-8337
This commit is contained in:
Brian Clozel
2017-02-20 15:03:26 +01:00
parent 21878f8528
commit 0b162e894b
5 changed files with 194 additions and 23 deletions

View File

@@ -18,12 +18,16 @@ package org.springframework.boot.autoconfigure.condition;
import org.junit.After;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.webflux.MockReactiveWebServerFactory;
import org.springframework.boot.context.embedded.ReactiveWebApplicationContext;
import org.springframework.boot.context.embedded.ReactiveWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -33,7 +37,7 @@ import static org.assertj.core.api.Assertions.entry;
/**
* Tests for {@link ConditionalOnNotWebApplication}.
*
* @author Dave Syer$
* @author Dave Syer
* @author Stephane Nicoll
*/
public class ConditionalOnNotWebApplicationTests {
@@ -61,7 +65,7 @@ public class ConditionalOnNotWebApplicationTests {
@Test
public void testNotWebApplicationWithReactiveContext() {
ReactiveWebApplicationContext ctx = new ReactiveWebApplicationContext();
ctx.register(NotWebApplicationConfiguration.class);
ctx.register(ReactiveApplicationConfig.class, NotWebApplicationConfiguration.class);
ctx.refresh();
this.context = ctx;
@@ -79,6 +83,20 @@ public class ConditionalOnNotWebApplicationTests {
entry("none", "none"));
}
@Configuration
protected static class ReactiveApplicationConfig {
@Bean
public ReactiveWebServerFactory reactiveWebServerFactory() {
return new MockReactiveWebServerFactory();
}
@Bean
public HttpHandler httpHandler() {
return (request, response) -> Mono.empty();
}
}
@Configuration
@ConditionalOnNotWebApplication
protected static class NotWebApplicationConfiguration {

View File

@@ -18,13 +18,17 @@ package org.springframework.boot.autoconfigure.condition;
import org.junit.After;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.webflux.MockReactiveWebServerFactory;
import org.springframework.boot.context.embedded.ReactiveWebApplicationContext;
import org.springframework.boot.context.embedded.ReactiveWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -118,6 +122,15 @@ public class ConditionalOnWebApplicationTests {
return "reactive";
}
@Bean
public ReactiveWebServerFactory reactiveWebServerFactory() {
return new MockReactiveWebServerFactory();
}
@Bean
public HttpHandler httpHandler() {
return (request, response) -> Mono.empty();
}
}
}