gpt4 book ai didi

java - Spring Boot 同步启动器

转载 作者:搜寻专家 更新时间:2023-11-01 00:56:15 26 4
gpt4 key购买 nike

org.springframework.boot.loader.Launcher 默认情况下总是会在 launch() 方法中生成一个后台线程 (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java#L105)。有什么简单的方法可以将其更改为在同一线程中执行,以便我的服务启动器可以知道应用程序何时完全加载,而不是每次都立即成功退出。

最佳答案

我相信你可以这样注册监听器:

public static void main(String[] args) {
SpringApplication app = new SpringApplication(WsBootClientApplication.class);

app.addListeners(new ApplicationListener<ContextStartedEvent>() {
@Override
public void onApplicationEvent(ContextStartedEvent event) {
//do your stuff
}
});
app.run(args);
}

如果 ContextStartedEvent 不是适合您的事件,您可以将 ContextStartedEvent 替换为 ApplicationEvent。这更通用,将处理您的应用程序可以监听的所有上下文生命周期事件。

关于java - Spring Boot 同步启动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com