服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringBoot项目不占用端口启动的方法

SpringBoot项目不占用端口启动的方法

2021-05-25 13:27Jetyang Java教程

这篇文章主要介绍了SpringBoot项目不占用端口启动的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

springboot项目不占用端口启动

现在很多互联网公司或者项目,都使用springboot + springcloud,以微服务的形式来提供后台服务。而且既然是微服务,所涉及到的项目就会很多,服务器端口资源就会相当紧张。而且,其实有些项目,如定时任务等,是不需要对外提供服务,也就不需要占用服务器端口的。那么,在springboot项目中,怎么实现呢?其实很简单,如下:

?
1
2
3
4
5
6
7
8
@enablescheduling
@springbootapplication
public class application {
 
 public static void main(string[] args) {
 new springapplicationbuilder().sources(application.class).web(false).run(args);
 }
}

这样,项目可以正常启动,而且,这个项目是不占用端口的。一般适用于定时任务项目。

starting from spring boot 2.0

-web(false)/setwebenvironment(false) is deprecated and instead web-application-type can be used to specify
spring.main.web-application-type=none

?
1
2
3
4
5
6
7
8
9
@springbootapplication
public class springbootdisablewebenvironmentapplication {
 
  public static void main(string[] args) {
    new springapplicationbuilder(springbootdisablewebenvironmentapplication .class)
      .web(webapplicationtype.none) // .reactive, .servlet
      .run(args);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://my.oschina.net/u/2617082/blog/1924985

延伸 · 阅读

精彩推荐