Programmatically restart Spring Boot application / Refresh Spring Context

后端 未结 6 1607
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 15:43

I am trying to programmatically restart my Spring Application without having the user to intervene.

Basically, I have a page which allows to switch the mode of the a

6条回答
  •  感动是毒
    2020-12-03 16:00

    Below restart method will work.

    `@SpringBootApplication public class Application {

    private static ConfigurableApplicationContext context;
    
    public static void main(String[] args) {
        context = SpringApplication.run(Application.class, args);
    }
    
    public static void restart() {
        ApplicationArguments args = context.getBean(ApplicationArguments.class);
    
        Thread thread = new Thread(() -> {
            context.close();
            context = SpringApplication.run(Application.class, args.getSourceArgs());
        });
    
        thread.setDaemon(false);
        thread.start();
    }
    

    }`

提交回复
热议问题