美文网首页
Spring ApplicationEvent 生命周期管理

Spring ApplicationEvent 生命周期管理

作者: liuliuzo | 来源:发表于2020-11-23 15:52 被阅读0次
    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 事件已发生!` 
    

    相关文章

      网友评论

          本文标题:Spring ApplicationEvent 生命周期管理

          本文链接:https://www.haomeiwen.com/subject/cflviktx.html