欢迎您访问新疆栾骏商贸有限公司,公司主营电子五金轴承产品批发业务!
全国咨询热线: 400-8878-609

新闻资讯

常见问题

springboot启动时如何获取端口和项目名

作者:用户投稿2026-01-11 05:12:09
目录
  • springboot启动获取端口和项目名
    • 背景
    • 踩坑
    • 使用
    • 效果
  • 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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。