lyh
3 天以前 2ab86210fb27787cb1be8976286b9b827f90997f
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
package com.lxzn.auth.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
/**
 * @author Rong.Jia
 * @description: server 配置
 * @date 2019/02/19 15:24:22
 */
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
 
    @Value("${server.host}")
    private String ip;
    @Value("${server.port}")
    private int serverPort;
 
    public String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://" + ip + ":" + this.serverPort;
    }
 
    public int getPort() {
        return this.serverPort;
    }
 
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }
 
}