何时使用SpringApplicationBuilder?

SpringBoot项目不占用端口启动web

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

 

1服务器

2app

3微服务

4ui

5spa

6code

7ci

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

  }

}

相关文章
相关标签/搜索