springboot 2.4中,我們寫的程序可能是一個服務(wù)梗肝,不需要提供http服務(wù)的铺董,那么其實可以不用啟動內(nèi)置容器。若不想啟動內(nèi)置容器坝锰,需要將WebApplicationType設(shè)置為NONE 重付;有兩種實現(xiàn)方式
一,在程序啟動時指定
@RestController
@SpringBootApplication
public class Bootstrap {
public static void main(String[] args) {
System.setProperty("org.springframework.boot.logging.LoggingSystem", "org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");
SpringApplication application = new SpringApplication(Bootstrap.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
//SpringApplication.run(Bootstrap.class, args);
}
}
二,在配置中指定(applicaton.yml或application.properties)
參考官方網(wǎng)站文檔 官方網(wǎng)站配置文檔
spring:
main:
web-application-type: "none"
以上兩種方式都可以不啟動內(nèi)置容器