Programmatically shut down Spring Boot application

前端 未结 5 1251
误落风尘
误落风尘 2020-11-27 13:52

How can I programmatically shutdown a Spring Boot application without terminating the VM?

In other works, what is

5条回答
  •  甜味超标
    2020-11-27 14:09

    This will make sure that the SpringBoot application is closed properly and the resources are released back to the operating system,

    @Autowired
    private ApplicationContext context;
    
    @GetMapping("/shutdown-app")
    public void shutdownApp() {
    
        int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
        System.exit(exitCode);
    }
    

提交回复
热议问题