PrintHomeUrl.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.fjs.scenic.config;
  2. import lombok.Data;
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.boot.ApplicationArguments;
  5. import org.springframework.boot.ApplicationRunner;
  6. import org.springframework.boot.CommandLineRunner;
  7. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.ApplicationContextAware;
  10. import org.springframework.stereotype.Component;
  11. import java.net.InetAddress;
  12. import java.net.UnknownHostException;
  13. /**
  14. * @program: fjs-parent
  15. * @description: 打印首页地址
  16. * @author: lfj
  17. * @since: 2021-09-29 10:08
  18. **/
  19. @Component
  20. public class PrintHomeUrl implements CommandLineRunner, ApplicationContextAware {
  21. private static ApplicationContext ctx = null;
  22. @Override
  23. public void run(String... args) throws Exception {
  24. try {
  25. String host = InetAddress.getLocalHost().getHostAddress();
  26. TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) ctx
  27. .getBean("tomcatServletWebServerFactory");
  28. int port = tomcatServletWebServerFactory.getPort();
  29. String contextPath = tomcatServletWebServerFactory.getContextPath();
  30. System.out.println("---------启动成功,访问: http://" + host + ":" + port + contextPath + "/");
  31. } catch (UnknownHostException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. @Override
  36. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  37. ctx = applicationContext;
  38. }
  39. }