Loading

05、SpringBoot事件监听

Java中的事件监听机制定义了以下几个角色:

  1. 事件:Event,继承 java.util.EventObject 类的对象
  2. 事件源:Source ,任意对象Object
  3. 监听器:Listener,实现 java.util.EventListener 接口 的对象

SpringBoot 在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成一些操作:

ApplicationContextInitializer、SpringApplicationRunListener、CommandLineRunner、ApplicationRunner

自定义监听器的启动时机:ApplicationRunner和CommandLineRunner都是当项目启动后执行,使用@Component放入容器即可使用。

ApplicationRunner使用案例:

package com.itheima.springbootlistener.listener;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;

/**
 * 当项目启动后执行run方法。
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner...run");
        System.out.println(Arrays.asList(args.getSourceArgs()));
    }
}

CommandLineRunner使用案例:

package com.itheima.springbootlistener.listener;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;


/**
 * 当项目启动后执行run方法。
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner...run");
        System.out.println(Arrays.asList(args));
    }
}

ApplicationContextInitializer使用案例:

需要在src/main/resources/META-INF/spring.factories中指定:

org.springframework.context.ApplicationContextInitializer=com.itheima.springbootlistener.listener.MyApplicationContextInitializer
package com.itheima.springbootlistener.listener;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 当IOC容器初始化之前,可用于检测一些资源是否存在。
 */
@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("ApplicationContextInitializer....initialize");
    }
}

SpringApplicationRunListener使用案例:

需要在src/main/resources/META-INF/spring.factories中指定:

org.springframework.boot.SpringApplicationRunListener=com.itheima.springbootlistener.listener.MySpringApplicationRunListener

且必须有带参构造方法:

public MySpringApplicationRunListener(SpringApplication application, String[] args) {}
package com.itheima.springbootlistener.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

public class MySpringApplicationRunListener implements SpringApplicationRunListener {

    public MySpringApplicationRunListener(SpringApplication application, String[] args) {

    }

    @Override
    public void starting() {
        System.out.println("starting...项目启动中");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("environmentPrepared...环境对象开始准备");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("contextPrepared...上下文对象开始准备");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("contextLoaded...上下文对象开始加载");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("started...上下文对象加载完成");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("running...项目启动完成,开始运行");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("failed...项目启动失败");
    }
}

 执行的生命周期:

com.itheima.springbootlistener.SpringbootListenerApplication
starting...项目启动中
environmentPrepared...环境对象开始准备
 .----------------.  .----------------.  .----------------.
| .--------------. || .--------------. || .--------------. |
| |  ____  ____  | || |  ____  ____  | || |     _____    | |
| | |_  _||_  _| | || | |_  _||_  _| | || |    |_   _|   | |
| |   \ \  / /   | || |   \ \  / /   | || |      | |     | |
| |    \ \/ /    | || |    \ \/ /    | || |   _  | |     | |
| |    _|  |_    | || |    _|  |_    | || |  | |_' |     | |
| |   |______|   | || |   |______|   | || |  `.___.'     | |
| |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------' 
ApplicationContextInitializer....initialize
contextPrepared...上下文对象开始准备
2023-02-12 00:05:53.758  INFO 3428 --- [           main] c.i.s.SpringbootListenerApplication      : Starting SpringbootListenerApplication on DESKTOP-827ESQR with PID 3428 (D:\IdeaProjects\springboot-demo\springboot02\springboot-listener\target\classes started by yyj in D:\IdeaProjects\springboot-demo\springboot02)
2023-02-12 00:05:53.760  INFO 3428 --- [           main] c.i.s.SpringbootListenerApplication      : No active profile set, falling back to default profiles: default
contextLoaded...上下文对象开始加载
2023-02-12 00:05:54.049  INFO 3428 --- [           main] c.i.s.SpringbootListenerApplication      : Started SpringbootListenerApplication in 0.453 seconds (JVM running for 0.778)
started...上下文对象加载完成
ApplicationRunner...run
[]
CommandLineRunner...run
[]
running...项目启动完成,开始运行

Process finished with exit code 0

 

 

 

posted @ 2023-02-12 00:20  1640808365  阅读(43)  评论(0编辑  收藏  举报