import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
@SpringBootApplication
public class Sp03UserServiceApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext context = SpringApplication.run(Sp03UserServiceApplication.class, args);
// 获取app的 访问地址
Environment environment = context.getBean(Environment.class);
//environment.getProperty("server.servlet.context-path") 应用的上下文路径,也可以称为项目路径
String host = InetAddress.getLocalHost().getHostAddress();
String port = environment.getProperty("server.port");
String path = environment.getProperty("server.servlet.context-path");
if (StringUtils.isEmpty(path)) {
path = "";
}
log.info("\n访问地址:http://{}:{}{}", host, port, path);
System.out.println("\n访问地址:http://"+host+":"+port+path);
System.out.println(port);
}
}
springboot项目启动后获取访问地址
最新推荐文章于 2024-07-11 09:26:39 发布