package com.liuliu.learning;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;
/**
* @author liuliu
*/
@Component
public class ApplicationEventListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
System.out.println("====" + event.getClass().getSimpleName() + " 事件已发生!");
} else if (event instanceof ContextRefreshedEvent) {
System.out.println("====" + event.getClass().getSimpleName() + " 事件已发生!");
} else if (event instanceof ContextStartedEvent) {
System.out.println("====" + event.getClass().getSimpleName() + " 事件已发生!");
} else if (event instanceof ContextStoppedEvent) {
System.out.println("====" + event.getClass().getSimpleName() + " 事件已发生!");
} else {
System.out.println("====" + "有其它事件发生:" + event.getClass().getName());
}
}
}
输出
` ====ContextRefreshedEvent 事件已发生!`
` ====有其它事件发生:org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent`
` ====有其它事件发生:org.springframework.boot.context.event.ApplicationStartedEvent`
` ====有其它事件发生:org.springframework.boot.context.event.ApplicationReadyEvent`
` ====ContextClosedEvent 事件已发生!`
网友评论