欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程

 更新時(shí)間:2019年09月10日 15:43:46   作者:加林  
這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

現(xiàn)在有一個(gè)非web項(xiàng)目: 比如一個(gè)kafka的消費(fèi)端, 監(jiān)聽某個(gè)消息topic, 并處理對(duì)應(yīng)的消息通過其他api發(fā)送通知或者短信給相關(guān)人員. 這時(shí)候想使用Spring Boot運(yùn)行, 但又不需要依賴web, tomcat, jdbc這些

依賴如下:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 <exclusions>
  <exclusion>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-to-slf4j</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.slf4j</groupId>
   <artifactId>jul-to-slf4j</artifactId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.springframework.kafka</groupId>
 <artifactId>spring-kafka</artifactId>
</dependency>

這時(shí)候 SpringBootApplication 的程序入口在執(zhí)行完main方法后直接exit了, 現(xiàn)在需要hold應(yīng)用程序防止直接退出, 有兩種方法:

  • 實(shí)現(xiàn) CommandLineRunner 接口在run方法中通過 Thread.currentThread().join() 使得應(yīng)用程序在執(zhí)行run方法時(shí)阻塞, 這樣程序就可以保持運(yùn)行
  • 通過 Spring Boot 提供的配置(推薦):
    • Spring Boot 2.0.0 以上的版本: spring.main.web-application-type=NONE // REACTIVE, SERVLET
    • Spring Boot 2.0.0 之前的版本: spring.main.web-environment=false

參考:

  1. SpringBoot 非 Web 項(xiàng)目運(yùn)行
  2. Spring Boot without the web server

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論