Spring Boot Application 事件和监听器

一:Application 事件

(1)ApplicationStartingEvent

  An ApplicationStartingEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.

 (2)ApplicationEnvironmentPreparedEvent

An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.

示例:

public class MyListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent  event) {
        SpringApplication app = event.getSpringApplication();
//        app.setBannerMode(Banner.Mode.OFF);
        System.out.println("#####MyListener found!#####");
    }
}

Application类中添加

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication app = new SpringApplication(Application.class);
        app.addListeners(new MyListener());
        app.run(args);
    }
}

运行结果:

 

 (3)ApplicationPreparedEvent

    An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.

(4)ApplicationReadyEvent 

    An ApplicationReadyEvent is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests.

当Application启动好后调用

示例:

public class MyListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent  event) {
        System.out.println("#####MyListener found!#####");
    }
}

 

运行结果位置:

 

(5)ApplicationFailedEvent 

    An ApplicationFailedEvent is sent if there is an exception on startup.

这里举了两种事件的例子,其他3种事件可以通过改写MyListener中参数调试。

转载于:https://www.cnblogs.com/fdzfd/p/7872909.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值