以编程方式重启Spring

以编程方式重启Spring

本文介绍了以编程方式重启Spring Boot应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot,并且有一个用例,用户可以上载一个文件,这会导致应用程序重新启动(因为在创建多个bean时使用了用户的上载).我知道我可以避免重新启动应用程序,但是此刻-这就是我想要的.

I'm using Spring Boot and I've got a use case where user can upload a file which should cause a restart of application (since user's upload is used during creation of multiple beans). I know I can avoid restarting the application, but at the moment - this is what I want.

我发现,但似乎没有触发ApplicationPreparedEvent.还有什么其他方法可以通过编程方式重启Spring Boot应用程序?

I've found RestartEndpoint in Spring-Cloud project, but it doesn't seem like ApplicationPreparedEvent is fired. Is there any other way I can programmatically restart Spring Boot application?

推荐答案

最简单的方法是调用 refresh() 方法.这将杀死并重新加载所有bean,因此应确保只有在应用程序安全的情况下才发生这种情况.

The simplest way to do this by calling the refresh() method on the Spring ApplicationContext. This will kill and reload all of your beans, so you should be certain that this occurs only when it is safe for your application to do so.

这篇关于以编程方式重启Spring Boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:47