From 303768b2b52ca01b58675a474748ee617561af84 Mon Sep 17 00:00:00 2001 From: John Franco Date: Tue, 11 Aug 2020 22:39:30 -0700 Subject: [PATCH 1/2] Harmonize Discover the HTTP Port at Runtime How-To See gh-22898 --- .../spring-boot-docs/src/main/asciidoc/howto.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 684dda5736..78870c6fb3 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -498,7 +498,7 @@ To scan for a free port (using OS natives to prevent clashes) use `server.port=0 [[howto-discover-the-http-port-at-runtime]] === Discover the HTTP Port at Runtime You can access the port the server is running on from log output or from the `ServletWebServerApplicationContext` through its `WebServer`. -The best way to get that and be sure that it has been initialized is to add a `@Bean` of type `ApplicationListener` and pull the container out of the event when it is published. +The best way to get that and be sure that it has been initialized is to add a `@Bean` of type `ApplicationListener` (or `ApplicationListener` for reactive servers) and pull the container out of the event when it is published. Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can also inject the actual port into a field by using the `@LocalServerPort` annotation, as shown in the following example: From ee2363fca724677b077311f0e5a8700fc20784cf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 12 Aug 2020 09:49:16 +0200 Subject: [PATCH 2/2] Polish "Harmonize Discover the HTTP Port at Runtime How-To" See gh-22898 --- .../spring-boot-docs/src/main/asciidoc/howto.adoc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 78870c6fb3..30af1d3440 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -497,8 +497,8 @@ To scan for a free port (using OS natives to prevent clashes) use `server.port=0 [[howto-discover-the-http-port-at-runtime]] === Discover the HTTP Port at Runtime -You can access the port the server is running on from log output or from the `ServletWebServerApplicationContext` through its `WebServer`. -The best way to get that and be sure that it has been initialized is to add a `@Bean` of type `ApplicationListener` (or `ApplicationListener` for reactive servers) and pull the container out of the event when it is published. +You can access the port the server is running on from log output or from the `WebServerApplicationContext` through its `WebServer`. +The best way to get that and be sure it has been initialized is to add a `@Bean` of type `ApplicationListener` and pull the container out of the event when it is published. Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can also inject the actual port into a field by using the `@LocalServerPort` annotation, as shown in the following example: @@ -507,9 +507,6 @@ Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class MyWebIntegrationTests { - @Autowired - ServletWebServerApplicationContext server; - @LocalServerPort int port;