SpringBoot2.X之旅,实现ApplicationListener,监听程序启动过程(Web Project)

背景需求:

由于项目启动时,需要自动加载初始化一些数据,这时候可以使用监听类

一、定义监听类,实现ApplicationListener接口:

package com.cobra.webdemo.listener;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
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;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/2317:16
 */
public class ApplicationEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent){
            System.out.println("环境初始化!!!");
        } else if (event instanceof ApplicationPreparedEvent){
            System.out.println("初始化完成!!!");
        } else if (event instanceof ContextRefreshedEvent) {
            System.out.println("应用刷新!!");
        } else if (event instanceof ApplicationReadyEvent) {
            System.out.println("项目启动完成!!");
        } else if (event instanceof ContextStartedEvent) {
            System.out.println("应用启动!!");
        } else if (event instanceof ContextStoppedEvent) {
            System.out.println("项目停止!!");
        } else if (event instanceof ContextClosedEvent) {
            System.out.println("应用关闭!!");
        }
    }

}

二、监听类注入容器,添加监听:

方式一,在application.yml配置文件配置:

#配置启动监听
context:
  listener:
    classes: com.cobra.webdemo.listener.ApplicationEventListener

方式二,在启动类main函数添加监听:

package com.cobra.webdemo;

import com.cobra.webdemo.listener.ApplicationEventListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebdemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(WebdemoApplication.class);
        app.addListeners(new ApplicationEventListener());
        app.run(args);
    }

}

测试:

方式三,直接用@Component注入bean,这种方法只能监听刷新和启动完成:

package com.cobra.webdemo.listener;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
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: Baron
 * @Description:
 * @Date: Created in 2019/3/2317:16
 */
@Component
public class ApplicationEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent){
            System.out.println("环境初始化!!!");
        } else if (event instanceof ApplicationPreparedEvent){
            System.out.println("初始化完成!!!");
        } else if (event instanceof ContextRefreshedEvent) {
            System.out.println("应用刷新!!");
        } else if (event instanceof ApplicationReadyEvent) {
            System.out.println("项目启动完成!!");
        } else if (event instanceof ContextStartedEvent) {
            System.out.println("应用启动!!");
        } else if (event instanceof ContextStoppedEvent) {
            System.out.println("项目停止!!");
        } else if (event instanceof ContextClosedEvent) {
            System.out.println("应用关闭!!");
        }
    }

}

测试:

三、拓展,还可以使用通过继承ApplicationEvent添加自定义的事件,供程序监听

猜你喜欢

转载自blog.csdn.net/weixin_37138899/article/details/88765759