SpringBoot自動重啟的兩種方法
更新時間:2023年12月08日 09:17:54 作者:Fisher3652
我們在項目開發(fā)階段,可能經(jīng)常會修改代碼,修改完后就要重啟Spring Boot,本文主要介紹了SpringBoot自動重啟的兩種方法,具有一定的參考價值,感興趣的可以了解一下
方法一:使用SpringCloud RestartEndpoint
- 使用的jar
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.cloud:spring-cloud-starter-config:3.0.7'
- 在application.properties添加配置
management.endpoint.restart.enabled=true spring.cloud.config.enabled=false
- 重啟方法的Controller
import javax.annotation.Resource; import org.springframework.cloud.context.restart.RestartEndpoint; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/restart") public class RestartController { @Resource private RestartEndpoint restartEndpoint; @GetMapping("/restartApplication") public void restartApplication() { restartEndpoint.restart(); } }
方法二:重新創(chuàng)建ApplicationContext上下文
- 啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class Application { public static ConfigurableApplicationContext context; public static void main(String[] args) { context = SpringApplication.run(Application.class); } }
- 重啟方法的Controller
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.SpringApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController @RequestMapping("/restart") public class RestartController { private static void restart() { ApplicationArguments args = Application.context.getBean(ApplicationArguments.class); Thread thread = new Thread(() -> { log.info("springboot restart..."); Application.context.close(); Application.context = SpringApplication.run(Application.class, args.getSourceArgs()); }); // 設(shè)置為用戶線程,不是守護線程 thread.setDaemon(false); thread.start(); } }
到此這篇關(guān)于SpringBoot自動重啟的兩種方法的文章就介紹到這了,更多相關(guān)SpringBoot自動重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能
這篇文章主要介紹了Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01java.lang.NoClassDefFoundError錯誤解決辦法
這篇文章主要介紹了java.lang.NoClassDefFoundError錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06Mybatis中xml的動態(tài)sql實現(xiàn)示例
本文主要介紹了Mybatis中xml的動態(tài)sql實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06