我是靠谱客的博主 拼搏指甲油,这篇文章主要介绍Spring boot项目启动获取项目地址,现在分享给大家,希望可以做个参考。

缘由是因为每次启动项目可能端口号会和本地某些端口号冲突,冲突后改application.properties,项目可能还有比较长的context-path

每次输入感觉很烦,手残经常出错

遂在项目启动时打印出项目主页地址即可

  private static ApplicationContext ctx;

    public static void main(String[] args) {
        ctx = SpringApplication.run(PlatformApplication.class, args);

        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();
        }
    }

打印项目启动加载的bean 为查错误

        String[] beanNames = ctx.getBeanDefinitionNames();

        System.out.println("所有beanNames个数:" + beanNames.length);

        for (String bn : beanNames) {
            System.out.println(bn);

        }

指出IDEA或者Spring boot 项目有一个坑,用maven启动项目后再用原生启动没有clean项目的情况下可能出现application.properties文件找不到,遂采用默认空配置

即端口号为8080,项目启动了但是你找不到!

最后

以上就是拼搏指甲油最近收集整理的关于Spring boot项目启动获取项目地址的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部