一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

springboot启动时获取端口和项目名代码示例

时间:2021-11-19 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下springboot启动时获取端口和项目名代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

springboot启动获取端口和项目名

背景

项目启动每次都要手动输url在浏览器中访问,就想能和vue项目一样启动能直接在控制台打印出url

踩坑

在项目中获取配置文件的方法为@Value,但是在启动类中无法使用,获取到的全都为null

使用

Environment

 public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        Environment environment = context.getBean(Environment.class);
		System.out.println("访问链接:http://localhost:" +environment.getProperty("server.port")+environment.getProperty("server.servlet.context-path");
      );
 }

效果

springboot配置项目运行端口号

可以通过application.properties配置文件来实现,添加一行代码即可

这个方法极其简洁

server.port=8084

热门栏目