如何在Spring应用程序初始化时获取tomcat端口?

时间:2016-07-13 10:46:31

标签: java spring tomcat

我想知道tomcat用于我的Spring应用程序的端口。

获取我使用的地址:

InetAddress inetAddress = InetAddress.getLocalHost();
String host = inetAddress.getHostAddress()

但内部任何地方都没有端口信息。我内部未指定application.propertiesserver.host server.port,因此environment.getProperty("server.port")会返回null

我也试过this解决方案:

@Inject
private ServerProperties serverProperties;
...
serverProperties.getPort()

但它也会返回null

我读here我应该使用Socket#getLocalPort(),但我不知道如何获取Socket并且新实例返回默认值:-1

在捕获请求时还有一种方法可以做到:

ServletRequest.getLocalAddr();
ServletRequest.getLocalPort();

但在我的情况下它没用。

0 个答案:

没有答案