一、会触发的事件:
1.ApplicationStartingEvent
2.ApplicationEnvironmentPreparedEvent
3.ApplicationContextInitializedEvent
4.ApplicationPreparedEvent
5.ApplicationStartedEvent
6.AvailabilityChangeEvent
7.ApplicationReadyEvent
8.AvailabilityChangeEvent
9.ApplicationFailedEvent(可能)
其中前4种Event无法通过注册EventListener bean的方式来监听到,因为容器还没有启动完成,可以通过在启动类添加自定义Listener的方式来监听:
springApplication.addListeners(new EarlyEventListener());
其中EarlyEventListener的定义如下:
package cn.edu.tju.listener;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
public class EarlyEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
System.out.println("application starting event triggered......");
}
}
程序启动时会看到如下输出: