package com.jeesite.modules.config; import java.net.InetAddress; import java.net.UnknownHostException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; @Slf4j @Component public class BaseConfig implements ApplicationRunner { @Autowired private Environment environment; public static String server_port = "8980"; public static String server_ip = "127.0.0.1"; public static String url = ""; public static String username = ""; public static String password = ""; @Override public void run(ApplicationArguments args){ server_port = environment.getProperty("server.port"); url = environment.getProperty("jdbc.url"); username = environment.getProperty("jdbc.username"); password = environment.getProperty("jdbc.password"); //System.out.println("server_port="+server_port); server_ip = getIPAddress(); //System.out.println("server_ip="+server_ip); } public static String getIPAddress() { String ip = "127.0.0.1"; //获取IP地址 //用 getLocalHost() 方法创建的InetAddress的对象 try { InetAddress address = InetAddress.getLocalHost(); //System.out.println(address.getHostName());//主机名 //System.out.println(address.getCanonicalHostName());//主机别名 //System.out.println(address.getHostAddress());//获取IP地址 ip = address.getHostAddress(); //System.out.println("ip="+ip_address);//获取IP地址 //System.out.println("==============="); } catch (UnknownHostException e) { // TODO Auto-generated catch block //e.printStackTrace(); } return ip; } }