Commit 5c14e853 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #22898 from johncfranco

* pr/22898:
  Polish "Harmonize Discover the HTTP Port at Runtime How-To"
  Harmonize Discover the HTTP Port at Runtime How-To

Closes gh-22898
parents cdbb7f44 ee2363fc
...@@ -497,8 +497,8 @@ To scan for a free port (using OS natives to prevent clashes) use `server.port=0 ...@@ -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]] [[howto-discover-the-http-port-at-runtime]]
=== 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`. 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 that it has been initialized is to add a `@Bean` of type `ApplicationListener<ServletWebServerInitializedEvent>` and pull the container out of the event when it is published. The best way to get that and be sure it has been initialized is to add a `@Bean` of type `ApplicationListener<WebServerInitializedEvent>` 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: 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 ...@@ -507,9 +507,6 @@ Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyWebIntegrationTests { public class MyWebIntegrationTests {
@Autowired
ServletWebServerApplicationContext server;
@LocalServerPort @LocalServerPort
int port; int port;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment