Spring Boot下非web後臺長執行應用的實現
測試環境:Spring Boot 2.1.1.RELEASE
Spring Boot
下非web應用只需在application.properties
里加上:
spring.main.web-application-type=none
新增之後,應用將不再啟動web容器,在CommandLineRunner
執行完畢後會自動退出,若我們需要長時間執行在後臺的應用需在最後加上:new Scanner(System.in).nextLine()
,如:
@SpringBootApplication public class Netty4ServerApplication { public static void main(String[] args) { SpringApplication.run(Netty4ServerApplication.class, args); } @Bean CommandLineRunner novaServerRunner(NovaDecoder decoder, NovaEncoder encoder){ return p -> { //...業務邏輯 new Scanner(System.in).nextLine(); }; } }
這時候應用即可在後臺執行,在控制檯隨便輸入一行字按回車即可關閉應用。
原始碼地址:ofollow,noindex" target="_blank">https://github.com/wiselyman/netty4-server.git