SpringBoot自動重啟的兩種方法
更新時間:2023年12月08日 09:17:54 作者:Fisher3652
我們在項(xiàng)目開發(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è)置為用戶線程,不是守護(hù)線程
thread.setDaemon(false);
thread.start();
}
}到此這篇關(guān)于SpringBoot自動重啟的兩種方法的文章就介紹到這了,更多相關(guān)SpringBoot自動重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot集成JSR303參數(shù)校驗(yàn)的方法實(shí)現(xiàn)
這篇文章主要介紹了Springboot集成JSR303參數(shù)校驗(yàn)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
使用EasyPoi實(shí)現(xiàn)百萬級數(shù)據(jù)導(dǎo)出的性能優(yōu)化方案
Easypoi 功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員 就可以方便的寫出Excel導(dǎo)出,Excel模板導(dǎo)出,Excel導(dǎo)入,本文給大家介紹了使用EasyPoi實(shí)現(xiàn)百萬級數(shù)據(jù)導(dǎo)出的性能優(yōu)化方案,需要的朋友可以參考下2025-08-08
SpringBoot2零基礎(chǔ)到精通之profile功能與自定義starter
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時也是簡化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)profile功能與自定義starter2022-03-03
SpringBoot Maven 項(xiàng)目 pom 中的 plugin&n
本文詳細(xì)介紹了Spring Boot Maven項(xiàng)目打包成jar文件時使用的spring-boot-maven-plugin插件,深入探討了插件的配置元素,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01
MyBatis-Plus邏輯刪除實(shí)現(xiàn)過程
本文介紹了MyBatis-Plus如何實(shí)現(xiàn)邏輯刪除功能,包括自動填充字段、配置與實(shí)現(xiàn)步驟、常見應(yīng)用場景,并展示了如何使用remove方法進(jìn)行邏輯刪除,邏輯刪除通過修改字段值來標(biāo)記數(shù)據(jù)為刪除狀態(tài),保留數(shù)據(jù)歷史記錄,避免數(shù)據(jù)丟失2025-12-12

