package com.fjs.scenic.config; import lombok.Data; import org.springframework.beans.BeansException; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; /** * @program: fjs-parent * @description: 打印首页地址 * @author: lfj * @since: 2021-09-29 10:08 **/ @Component public class PrintHomeUrl implements CommandLineRunner, ApplicationContextAware { private static ApplicationContext ctx = null; @Override public void run(String... args) throws Exception { try { String host = InetAddress.getLocalHost().getHostAddress(); TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) ctx .getBean("tomcatServletWebServerFactory"); int port = tomcatServletWebServerFactory.getPort(); String contextPath = tomcatServletWebServerFactory.getContextPath(); System.out.println("---------启动成功,访问: http://" + host + ":" + port + contextPath + "/"); } catch (UnknownHostException e) { e.printStackTrace(); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ctx = applicationContext; } }