近期有一个需求提供一个系统安装的导航界面,所以需要动态设置一些配置文件的项,比如数据库的连接等。
所以需要自动重启一下项目来达到重载配置的效果。
启动类做如下操作
run方法返回一个 ConfigurableApplicationContext ,也就是上下文对象,上下文对象中有一个 close() 方法。
用另一个线程来重新启动项目就可以了。
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
idGeneratorConfig();
context=SpringApplication.run(CloudApplication.class, args);
}
public static void restartApplication() {
ApplicationArguments args = context.getBean(ApplicationArguments.class);
Thread thread = new Thread(() -> {
context.close();
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
context = SpringApplication.run(CloudApplication.class, args.getSourceArgs());
});
thread.setDaemon(false);
thread.start();
}
参考 https://www.sxkawzp.cn/archives/springbootinapplicationrestart
全部评论 (暂无评论)
info 还没有任何评论,你来说两句呐!