zoukankan      html  css  js  c++  java
  • SpringBoot加载应用事件监听器

    SpringBoot加载应用事件监听器

    利用 Spring 工厂加载机制,实例化 ApplicationListener 实现类,并排序对象集合

    创建应用事件监听器

    • 创建类实现接口ApplicationListener,可以使用@Order或实现Orderd接口进行排序
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class HelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println("HelloWorld : " + event.getApplicationContext().getId()
                    + " , timestamp : " + event.getTimestamp());
        }
    }
    
    public class AfterHelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent>,Ordered {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println("AfterHelloWorld : " + event.getApplicationContext().getId()
                    + " , timestamp : " + event.getTimestamp());
        }
        @Override
        public int getOrder() {
            return Ordered.LOWEST_PRECEDENCE;
        }
    }
    
    • 在spring.properties中配置
    # ApplicationListener
    org.springframework.context.ApplicationListener=
    com.imooc.diveinspringboot.listener.AfterHelloWorldApplicationListener,
    com.imooc.diveinspringboot.listener.HelloWorldApplicationListener,
    
    • 输出

    HelloWorld : application , timestamp : 1591105193644
    AfterHelloWorld : application , timestamp : 1591105193644

  • 相关阅读:
    软件测试流程
    软件测试第三天
    软件测试第二天
    软件测试第一天
    一起交流,共同进步
    GIF89a图片头文件欺骗
    spring boot 整合mybatis:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
    webService接口例子(转)
    爬虫篇 2017/12/22 暖冬
    2017/12/17 冷~~
  • 原文地址:https://www.cnblogs.com/fjf3997/p/13035875.html
Copyright © 2011-2022 走看看