[BAEL-4612] get spring boot port at runtime
This commit is contained in:
+12
@@ -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);
|
||||
}
|
||||
}
|
||||
+20
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user