微服务架构 | Spring - [启动]

@

Spring 启动相关的钩子

  • before - main
  • @PostConstruct
  • ApplicationListener<ApplicationStartedEvent>
  • ApplicationRunner.run()
  • commandLineRunner.run()
  • ApplicationListener<ApplicationReadyEvent>
  • after - main

实际使用中,只推荐使用

  • @PostConstruct
    广泛用于几乎所有 SpringBean 的初始化,推荐所有即时生效的对象在这里完成自定义构建,比如具有状态的容器,或需要进行初始化并即时生效的对象
  • ApplicationRunner.run()
    可以用于允许延时加载的工作
posted @ 2025-05-20 15:14  问仙长何方蓬莱  阅读(6)  评论(0)    收藏  举报