Fork me on github

spring ApplicationListener 和 CommandLineRunner

ApplicationContext 事件机制是观察者设计模式的实现

如果容器中有一个ApplicationListener Bean,每当 ApplicationContext 发布 ApplicationEvent 时,ApplicationListener Bean将自动被触发。这种事件机制需要程序显式触发。

CommandLineRunner 接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。

直接继承,复写run方法即可,在run()方法里可以使用任何依赖,因为它们已经初始化好了

posted @ 2022-08-14 23:42  zjy4fun  阅读(79)  评论(0编辑  收藏  举报