zoukankan      html  css  js  c++  java
  • Spring Boot Application 事件和监听器

    https://www.cnblogs.com/fdzfd/p/7872909.html

    *****************************************************

    一: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中参数调试。

     

  • 相关阅读:
    hdu5593--ZYB's Tree(树形dp)
    poj1637--Sightseeing tour(最大流)
    Educational Codeforces Round 81 (Rated for Div. 2)
    【cf1286B】B. Numbers on Tree(贪心)
    【cf1285E】E. Delete a Segment(vector+二分)
    【cf1293E】E.Xenon's Attack on the Gangs(dp)
    Codeforces Round #609 (Div. 2)
    Educational Codeforces Round 78 (Rated for Div. 2)
    【bzoj4671】异或图(容斥+斯特林反演+线性基)
    【bzoj5339】[TJOI2018]教科书般的亵渎(拉格朗日插值/第二类斯特林数)
  • 原文地址:https://www.cnblogs.com/zhao1949/p/9165571.html
Copyright © 2011-2022 走看看