springboot-监听器

发布时间 2023-04-02 11:35:37作者: his365

监听器

  • ApplicationListener可以实现这个接口时传入对应的监听器,用于监听该事件
  • 比如:实现 ApplicationListener<ContextRefreshedEvent> 接口,重写 onApplicationEvent 方法,将 ContextRefreshedEvent 对象传进去。如果我们想在加载或刷新应用上下文时,也重新刷新下我们预加载的资源,就可以通过监听 ContextRefreshedEvent 来做这样的事情。
package cn.tjhis.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * 描述 : 在这里配置 \META-INF\spring.factories
 * 1 org.springframework.boot.context.event.ApplicationStartingEvent
 * 2 org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
 * 3 org.springframework.boot.context.event.ApplicationContextInitializedEvent
 * 4 org.springframework.boot.context.event.ApplicationPreparedEvent
 * 5 org.springframework.context.event.ContextRefreshedEvent
 * 6 org.springframework.boot.context.event.ApplicationStartedEvent
 * 7 org.springframework.boot.availability.AvailabilityChangeEvent
 * 8 org.springframework.boot.context.event.ApplicationReadyEvent
 * 9 org.springframework.boot.availability.AvailabilityChangeEvent
 *
 * <p>路径 : cn.tjhis.listener
 *
 * <p>工程 : autobean
 *
 * <p>作者 : wanghx
 *
 * <p>日期 : 2023-04-02 11:17
 *
 * @author : Administrator
 */
public class MyListener implements ApplicationListener {
    private int count=0;
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        count++;
        System.out.printf("监听到事件: %5d %-1s",count,event.getClass().getName());
        System.out.println("");
    }
}