SpringBoot项目不占用端口启动
现在很多互联网公司或者项目,都使用SpringBoot + SpringCloud,以微服务的形式来提供后台服务。而且既然是微服务,所涉及到的项目就会很多,服务器端口资源就会相当紧张。而且,其实有些项目,如定时任务等,是不需要对外提供服务,也就不需要占用服务器端口的。那么,在SpringBoot项目中,怎么实现呢?其实很简单,如下:
@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
@SpringBootApplication
public class SpringBootDisableWebEnvironmentApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SpringBootDisableWebEnvironmentApplication .class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
SpringCloud微服务(07):Zipkin组件,实现请求链路追踪
2026-05-17栏目: 教程
-
阿里云推出高性能一体机POLARDB BOX,全面兼容Oracle、mysql
2026-05-17栏目: 教程
-
利用java反射和java-parser制作可以迭代、分布式、全栈式代码生成器的研究
2026-05-17栏目: 教程
-
CI做到90%的行覆盖率,真能发现BUG吗?
2026-05-17栏目: 教程
-
深入浅出:Hadoop的start-balancer.sh与hdfs balancer分布式数据均衡
2026-05-17栏目: 教程
