简体   繁体   English

如何在Spring Boot的启动过程中关闭应用程序

[英]how to close the application during startup in spring boot

I have a question regarding the startup in spring boot , how to close the application during the startup time, for example, I have the following 我有一个关于spring boot ,如何在启动期间关闭应用程序,例如,我有以下内容

application.yml : application.yml

ansi:
   true

And I have the following @Configuration class: 我有以下@Configuration类:

@Configuration
class AppConfig {
   @Value('${ansi}')
   String ansi;


   @Bean
   getAnsi() {
        if(ansi.equals("true")) {
             Ansi ansiObj = new Ansi();
             ansiObj.ansi = ansi;
             return ansiObj;
        }
   }
}

class Ansi {
   String ansi;
}

When ansi in the application.yml is true , it continue, otherwise, the application should be closed, can we close the application during the bean creation? application.yml ansitrue ,它将继续,否则,应关闭该应用程序,我们可以在bean创建期间关闭该应用程序吗? is it a good practice? 这是一个好习惯吗? Are there any good ways to handle this? 有什么好的方法可以解决这个问题?

If a bean throws an exception then Spring will not proceed and the process will end. 如果bean抛出异常,那么Spring将不会继续进行,并且该过程将结束。

if(ansi.equals("true")) {
     Ansi ansiObj = new Ansi();
     ansiObj.ansi = ansi;
     return ansiObj;
}
else  {
    throw new IllegalArgumentException("reason");
}

I can't say that I've ever had a use-case for it, but I wouldn't say it's necessary bad practice. 我不能说我曾经有过用例,但我不会说这是必要的坏习惯。 In this limited example of true and false, it seems a bit unusual. 在这个对与错的有限示例中,这似乎有点不寻常。 It would make more sense if you needed a constraint on a property eg X < 10 如果您需要对属性进行约束,例如X <10

We have a lot of options to shutdown spring-boot application: 我们有很多选项可以关闭spring-boot应用程序:

Shutdown rest endpoint - add below properites to your application.properties and fire following request curl -X POST localhost:port/actuator/shutdown 关闭其余端点-在您的application.properties中添加以下属性,并在请求curl -X POST localhost:port/actuator/shutdown

management.endpoints.web.exposure.include=*  
management.endpoint.shutdown.enabled=true  
endpoints.shutdown.enabled=true

Also you can call suitable method to shutdown application: 您也可以调用合适的方法来关闭应用程序:

  • By calling method close() on ConfigurableApplicationContext object (it will close application context) 通过对ConfigurableApplicationContext对象调用close()方法(它将关闭应用程序上下文)
  • By passing exit code to method SpringApplication.exit(ctx, () -> 0); 通过将退出代码传递给方法SpringApplication.exit(ctx, () -> 0);

Please check this article for more details. 请查看文章了解更多详情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring启动应用程序中启动应用程序期间缓存数据 - How to cache data during application startup in Spring boot application 如何在 Spring Boot 应用程序启动期间将 SynchronizationCallbacks 添加到 @TransactionalEventListener? - How to add SynchronizationCallbacks to @TransactionalEventListener during spring boot application startup? Spring Boot 应用程序 + Jolokia - 启动时出现异常 - Spring Boot application + Jolokia - exception during startup 在Tomcat上启动Spring Boot应用程序期间出错(数据源无法实例化) - Error during startup of Spring Boot Application on Tomcat (Datasource fails to instantiate) 如何在 Spring Boot 的构建期间或启动期间验证注释? - How to validate annotations during build or during startup on Spring Boot? 应用程序启动失败-Spring Boot - Application startup failed - Spring Boot Spring Boot中的AspectJ在启动期间导致ClassNotFoundException - AspectJ in Spring Boot causes ClassNotFoundException during startup 如何根据属性有条件地使 Spring Boot 应用程序在启动时终止 - How to conditionally make a Spring Boot application terminate at startup based on a property 如何配置最新的 Spring Boot 应用程序以在启动时运行单元测试 - How to configure latest Spring Boot application to run unit tests at startup Spring 启动应用程序:如何在启动时使用flyway创建shema? - Spring Boot Application: how to create shema using flyway on startup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM