2017-08-15 53 views
0

語言:Java中,框架:Springboot,應用程序名稱:abcApplication 「21:47:32.133 [主] INFO ccabc.abcApplication - 在12.49秒開始abcApplication(JVM運行8.467)」如何捕捉「 - 在12.49秒內啓動abcApplication」事件?

我正在開發一個帶有Springboot的Web應用程序。 當應用程序啓動成功時,springboot-application會輸出上述消息。我該如何處理這個事件?我想在應用程序的初始化完成時執行一些操作。

回答

0

什麼你正在尋找的是ContextRefreshedEvent,你可以創建一個類,並使其成分並實現ApplicationListener

@Component 
public class ServerReadyClass implements ApplicationListener<ContextRefreshedEvent> { 

    @Override 
    public void onApplicationEvent(ContextRefreshedEvent event) { 
     //YOUR LOGIC 
    } 
} 
+0

非常感謝。這解決了。 :) – 16cha

0

你可以聽ApplicationReadyEvent了。

@Configuration 
public class ProjectConfiguration { 
    private static final Logger log = 
    LoggerFactory.getLogger(ProjectConfiguration.class); 

    @EventListener(ApplicationReadyEvent.class) 
    public void doSomethingAfterStartup() { 
    log.info("hello world, I have just started up"); 
    } 
} 
+0

非常感謝! – 16cha