繁体   English   中英

重启 Java Spring 启动应用程序

[英]Restarting Java Spring Boot Application

首先,我只想告诉我,我已经阅读了多种方法,但没有一个对我有用。 我只需要在调用方法时重新启动应用程序。

到目前为止我所尝试的:

@Service
    public class RestartService {

        @Autowired
        private RestartEndpoint restartEndpoint;

        public void restartApp() {
            Thread restartThread = new Thread(() -> restartEndpoint.restart());
            restartThread.setDaemon(false);
            restartThread.start();
        }
    }

另一个:

@SpringBootApplication
public class RestfulWebservicesApplication {


private static ConfigurableApplicationContext context;

    public static void main(String[] args) throws IOException {
        SpringApplication.run(RestfulWebservicesApplication.class, args);


    }

    public static void restart() {
        ApplicationArguments args = context.getBean(ApplicationArguments.class);

        Thread thread = new Thread(() -> {
            context.close();
            context = SpringApplication.run(RestfulWebservicesApplication.class, args.getSourceArgs());
        });

        thread.setDaemon(false);
        thread.start();
    }
}

和这个:

public class OneMoreRestart {

    @GetMapping("/restart")
    void restart() {
        Thread restartThread = new Thread(() -> {
            try {
                Thread.sleep(1000);
                Main.restart();
            } catch (InterruptedException ignored) {
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
    }

}

我也尝试了这里发布的方法: https://www.baeldung.com/java-restart-spring-boot-app

我已经尝试了我在谷歌或这里找到的所有方法,它们都不适合我....

我通常会得到空指针异常,我知道我是一个完整的菜鸟,但我从不认为重新启动应用程序可能会如此困难。 我在这里错过了什么或者我在这里做错了什么????

您需要修改它,如下所示

public static void main(String[] args) throws IOException {
    context = SpringApplication.run(RestfulWebservicesApplication.class, args);
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM