我需要向 Java Spring 应用程序添加一些日志记录,以记录该应用程序已重新启动,因此我们可以根据它的存在创建通知和 Hook 。任何人都可以建议最好的方法吗?我能找到的所有资源都在谈论 Java Bean 生命周期,而不是应用程序生命周期。
最佳答案
您可能有兴趣查看 Event Handling in Spring. 你必须 catch ContextStartedEvent
This event is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event.
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
关于java - 如何在 Java Spring 应用程序成功启动时添加日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919762/