yangys
2024-03-29 153cc3fd4ef015a8b1390b2eef3d102c5859a5e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.qianwen.core.launch.server;
 
import com.qianwen.core.launch.utils.INetUtil;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Configuration;
 
@Configuration(proxyBeanMethods = false)
public class ServerInfo implements SmartInitializingSingleton {
    private final ServerProperties serverProperties;
    private String hostName;
    private String ip;
    private Integer port;
    private String ipWithPort;
 
    public ServerProperties getServerProperties() {
        return this.serverProperties;
    }
 
    public String getHostName() {
        return this.hostName;
    }
 
    public String getIp() {
        return this.ip;
    }
 
    public Integer getPort() {
        return this.port;
    }
 
    public String getIpWithPort() {
        return this.ipWithPort;
    }
 
    @Autowired(required = false)
    public ServerInfo(ServerProperties serverProperties) {
        this.serverProperties = serverProperties;
    }
 
    public void afterSingletonsInstantiated() {
        this.hostName = INetUtil.getHostName();
        this.ip = INetUtil.getHostIp();
        this.port = this.serverProperties.getPort();
        this.ipWithPort = String.format("%s:%d", this.ip, this.port);
    }
}