SpringBoot2.2.x获取项目的ip和端口号
- 编写服务类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.cloud.route.V4.template.config; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; import java.net.InetAddress; import java.net.UnknownHostException; @Component public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> { private int serverPort; public String getUrl() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return "http://"+ address.getHostAddress() +":"+this.serverPort; } @Override public void onApplicationEvent(WebServerInitializedEvent event) { this.serverPort = event.getWebServer().getPort(); } }
- 业务层注入调用
复制代码
1
2
3
4
5
6
7@Autowired private ServerConfig serverConfig; public String getUrl() { return serverConfig.getUrl(); }
转载地址:https://blog.csdn.net/mibi8840/article/details/83824134
最后
以上就是碧蓝保温杯最近收集整理的关于SpringBoot2.2.x获取项目的ip和端口号的全部内容,更多相关SpringBoot2.2.x获取项目内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复