springboot添加启动事件监听
当我们需要在 springboot 启动时做的第一件事,可以通过添加事件监听实现,具体如下:
添加事件
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
public class MyEvent implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
// todo
}
}
添加监听
@SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(StartApplication.class);
app.addListeners(new MyEvent());
app.run(args);
}
}
本文来自博客园,作者:这个杀手冷死了,转载请注明原文链接:https://www.cnblogs.com/pandacode/p/springboot-tian-jia-qi-dong-shi-jian-jian-ting.html