java - 在 WebServer 启动前触发 ApplicationListener
问题描述
我有一个短暂的过程来将一个简单的文件从类路径读取到内存中,我选择ApplicationListener<ApplicationReadyEvent>
让 spring 触发一个任务:
@Component
@Priority(1)
class MyLoader implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
doStuff();
}
}
我意识到我在这个应用程序中提供的 HTTP 请求并不依赖于内存中的数据。如何MyLoader
在 Web 服务器准备好服务请求之前强制执行类?我不知道如何定义这种依赖关系?
解决方案
You should listen for an event before the ApplicationReadyEvent
, because it is the final one, marking that the application is ready to serve requests.
For example, you can listen to ApplicationStartingEvent
. See a list of the available ApplicationEvents here.
推荐阅读
- swift - 使用 Swift Package Manager 的 Swift IOS 库无法编译
- winforms - ContextMenuStrip 中的嵌套子菜单
- gitlab - GitlabCI:触发管道
- c# - 复杂的 linq 语句 - 嵌套组/where/sum/select
- snakemake - 如何从 config.yaml 分配多个路径?
- scala - 2个案例类的匹配值
- angular - 在 primeNg 下拉列表中使用 overlayVisible 属性
- groovy - 在groovy中以修剪格式获取数据库日期
- javascript - 当我调整浏览器大小时,为什么我的 div 会重叠?
- regex - AWS Cognito 密码正则表达式 - 特定于 AWS Cognito