[BAEL-4612] get spring boot port at runtime

This commit is contained in:
Kai Yuan 2020-09-22 00:41:23 +02:00
parent 10e7aaed73
commit 8b0b645e6f
6 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.serverport;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GetServerPortApplication {
public static void main(String[] args) {
SpringApplication.run(GetServerPortApplication.class, args);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.serverport;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
@Service
public class ServerPortService {
private int port;
public int getPort() {
return port;
}
@EventListener
public void onApplicationEvent(final ServletWebServerInitializedEvent event) {
port = event.getWebServer().getPort();
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.serverport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GetServerPortApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("fixedport")
public class GetServerFixedPortUnitTest {
private final static int EXPECTED_PORT = 7777;
@Value("${server.port}")
private int serverPort;
@Autowired
private ServerProperties serverProperties;
@Test
public void givenFixedPortAsServerPort_whenReadServerPort_thenGetThePort() {
assertEquals("Reading fixed port by @Value(\"${server.port}\") will get the port.", EXPECTED_PORT, serverPort);
}
@Test
public void givenFixedPortAsServerPort_whenReadServerProps_thenGetThePort() {
int port = serverProperties.getPort();
assertEquals("Reading fixed port from serverProperties will get the port.", EXPECTED_PORT, port);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.serverport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GetServerPortApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("randomport")
public class GetServerRandomPortUnitTest {
@Value("${server.port}")
private int randomServerPort;
@Autowired
private ServerPortService serverPortService;
@Autowired
private ServerProperties serverProperties;
@Autowired
private ServletWebServerApplicationContext webServerAppCtxt;
@Test
public void given0AsServerPort_whenReadServerPort_thenGet0() {
assertEquals("Reading random port by @Value(\"${server.port}\") will get 0.", 0, randomServerPort);
}
@Test
public void given0AsServerPort_whenReadServerProps_thenGet0() {
int port = serverProperties.getPort();
assertEquals("Reading random port by serverProperties will get 0.", 0, port);
}
@Test
public void given0AsServerPort_whenReadWebAppCtxt_thenGetThePort() {
int port = webServerAppCtxt.getWebServer().getPort();
assertTrue("The random port should be greater than 1023", port > 1023);
}
@Test
public void given0AsServerPort_whenReadFromListener_thenGetThePort() {
int port = serverPortService.getPort();
assertTrue("The random port should be greater than 1023", port > 1023);
}
}