One way I found to do is from org.springframework.boot.web.embedded.netty.NettyWebServer#start
and looks like a listener to a certain event:
@Component
@Slf4j
public class ServerStartListener implements ApplicationListener<WebServerInitializedEvent> {
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
// This is to exclude management port
if (!"management".equals(event.getApplicationContext().getServerNamespace())) {
log.info("Application started on port {}", event.getWebServer().getPort());
}
}
}
However, I find this not very elegant and wonder if there are better ways.