栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

以编程方式重新启动Spring Boot应用程序/刷新Spring上下文

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以编程方式重新启动Spring Boot应用程序/刷新Spring上下文

即使Alex的解决方案 有效 ,我也不相信仅包含2个依赖项(

Actuator
CloudContext
)就能够执行一项操作。相反,我结合了他的答案并修改了我的代码,以执行我想要的操作。

因此,首先,使用和执行代码 至关重要 。我有以下处理重启的端点方法:

new Thread()``setDaemon(false);

val restartThread = Thread {    logger.info("Restarting...")    Thread.sleep(1000)    SpringMain.restartToMode(AppMode.valueOf(change.newMode.toUpperCase()))    logger.info("Restarting... Done.")}restartThread.isDaemon = falserestartThread.start()

Thread.sleep(1000)
不是必需的,但我希望我的控制器输出实际上重新启动应用程序前视图。

SpringMain.restartToMode
具有以下内容:

@Synchronized fun restartToMode(mode: AppMode) {    requireNotNull(context)    requireNotNull(application)    // internal logic to potentially produce a new arguments array    // close previous context    context.close()    // and build new one using the new mode    val builder = SpringApplicationBuilder(SpringMain::class.java)    application = builder.application()    context = builder.build().run(*argsArray)}

context
application
来自
main
在启动应用程序的方法:

val args = ArrayList<String>()lateinit var context: ConfigurableApplicationContextlateinit var application: SpringApplication@Throws(Exception::class)@JvmStatic fun main(args: Array<String>) {    this.args += args    val builder = SpringApplicationBuilder(SpringMain::class.java)    application = builder.application()    context = builder.build().run(*args)}

我不确定这是否会引起任何问题。如果有,我将更新此答案。希望这对其他人有帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号