Spring 启动 : exit jvm on "Application run failed"

标签 spring spring-boot

有没有办法对“应用程序运行失败”使用react,例如万一数据库不可用?

在我的例子中,期望的行为是退出 JVM 进程,这样 docker 容器将自动重启

我尝试监听“ContextClosedEvent”,但它不适用于启动失败的情况。

最佳答案

对于任何需要在启动过程中出现任何故障时终止应用程序的人(kotlin 中的示例):

@SpringBootApplication
class MyApplication

fun main(args: Array<String>) {
    val application = SpringApplication()
    application.addListeners(FailedInitializationMonitor())
    application.addPrimarySources(listOf(MyApplication::class.java))
    application.run(*args)
}

class FailedInitializationMonitor : ApplicationListener<ApplicationFailedEvent> {

    override fun onApplicationEvent(event: ApplicationFailedEvent) {
        exitProcess(1)
    }
    
}

关于 Spring 启动 : exit jvm on "Application run failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350441/

相关文章:

java - Spring Data JPA 存储库 findAll() 空指针

java - 一次连续向10000个客户端发布数据

java - 在特定gradle任务上设置调试JVM args

Java hibernate : Access foregin key

spring - 未能延迟初始化集合

Spring JDBC - 防止日志记录 - INFO : Added default SqlReturnUpdateCount parameter named #update-count-1

spring - @ControllerAdvice 单元测试 Controller 中的 Autowired MessageSource null

java - Spring 启动: Create EAR from springboot multi-module project

spring - EnableBinding 类型已弃用,StreamListener 类型已弃用 - Spring Cloud Stream

java - @PropertySource 中的类路径通配符