W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
HTTP 服務(wù)器發(fā)出許多 Bean 事件,定義在 io.micronaut.runtime.server.event 包中,您可以為其編寫偵聽器。下表總結(jié)了這些:
事件 | 描述 |
---|---|
服務(wù)器完成啟動時發(fā)出 |
|
服務(wù)器關(guān)閉時發(fā)出 |
|
在調(diào)用所有 ServerStartupEvent 偵聽器并公開 EmbeddedServerInstance 后發(fā)出 |
|
在調(diào)用所有 ServerShutdownEvent 偵聽器并公開 EmbeddedServerInstance 后發(fā)出 |
在 ServerStartupEvent 的偵聽器中做大量工作會增加啟動時間。
以下示例定義了一個偵聽 ServerStartupEvent 的 ApplicationEventListener:
監(jiān)聽服務(wù)器啟動事件
import io.micronaut.context.event.ApplicationEventListener;
...
@Singleton
public class StartupListener implements ApplicationEventListener<ServerStartupEvent> {
@Override
public void onApplicationEvent(ServerStartupEvent event) {
// logic here
...
}
}
或者,您也可以在接受 ServerStartupEvent 的任何 bean 的方法上使用 @EventListener 注釋:
將@EventListener 與 ServerStartupEvent 一起使用
import io.micronaut.runtime.event.annotation.EventListener;
import io.micronaut.runtime.server.event.ServerStartupEvent;
import javax.inject.Singleton;
...
@Singleton
public class MyBean {
@EventListener
public void onStartup(ServerStartupEvent event) {
// logic here
...
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: