当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot加载应用事件监听器代码实例

SpringBoot加载应用事件监听器代码实例

2020年06月14日  | 移动技术网IT编程  | 我要评论

杭州同言堂聊天室,沈阳市天气预报,郓城帝景湾

利用 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网