Spring Boot 中,监听应用程序启动的生命周期事件的4种方法

2024-07-21 1276阅读

文章目录

  • 前言
    • 在 Spring Boot 中,监听应用程序启动的生命周期事件有多种方法。你可以使用以下几种方式来实现:
    • 一、使用 ApplicationListener
    • 二、使用 @EventListener
    • 三、实现 CommandLineRunner 或 ApplicationRunner
    • 四、使用 SmartLifecycle
    • 总结

      前言

      在 Spring Boot 中,监听应用程序启动的生命周期事件有多种方法。你可以使用以下几种方式来实现:

      一、使用 ApplicationListener

      你可以创建一个实现 ApplicationListener 接口的类,监听 ApplicationStartingEvent、ApplicationEnvironmentPreparedEvent、ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent 等事件。

      Spring Boot 中,监听应用程序启动的生命周期事件的4种方法
      (图片来源网络,侵删)
      import org.springframework.boot.context.event.ApplicationReadyEvent;
      import org.springframework.context.ApplicationListener;
      import org.springframework.stereotype.Component;
      @Component
      public class ApplicationStartupListener implements ApplicationListener {
          @Override
          public void onApplicationEvent(ApplicationReadyEvent event) {
              System.out.println("Application is ready!");
              // Your custom logic here
          }
      }
      

      二、使用 @EventListener

      你可以在一个 Spring Bean 中使用 @EventListener 注解来监听这些事件。

      import org.springframework.boot.context.event.ApplicationReadyEvent;
      import org.springframework.context.event.EventListener;
      import org.springframework.stereotype.Component;
      @Component
      public class ApplicationStartupListener {
          @EventListener
          public void handleApplicationReady(ApplicationReadyEvent event) {
              System.out.println("Application is ready!");
              // Your custom logic here
          }
      }
      

      三、实现 CommandLineRunner 或 ApplicationRunner

      这两个接口允许你在 Spring Boot 应用启动完成之后运行一些特定的代码。

      • CommandLineRunner 接收一个 String 数组作为参数。
      • ApplicationRunner 接收一个 ApplicationArguments 对象作为参数。
        import org.springframework.boot.CommandLineRunner;
        import org.springframework.stereotype.Component;
        @Component
        public class MyCommandLineRunner implements CommandLineRunner {
            @Override
            public void run(String... args) throws Exception {
                System.out.println("Application has started!");
                // Your custom logic here
            }
        }
        

        四、使用 SmartLifecycle

        如果你需要更精细的控制,可以实现 SmartLifecycle 接口。这提供了一个 isAutoStartup 方法,可以控制组件是否自动启动,以及 stop 和 start 方法,可以控制组件的停止和启动。

        import org.springframework.context.SmartLifecycle;
        import org.springframework.stereotype.Component;
        @Component
        public class MySmartLifecycle implements SmartLifecycle {
            private boolean running = false;
            @Override
            public void start() {
                System.out.println("Starting MySmartLifecycle");
                running = true;
                // Your custom logic here
            }
            @Override
            public void stop() {
                System.out.println("Stopping MySmartLifecycle");
                running = false;
                // Your custom logic here
            }
            @Override
            public boolean isRunning() {
                return running;
            }
            @Override
            public int getPhase() {
                return 0;
            }
            @Override
            public boolean isAutoStartup() {
                return true;
            }
            @Override
            public void stop(Runnable callback) {
                stop();
                callback.run();
            }
        }
        

        总结

        根据你的需求,你可以选择以上任意一种方式来监听 Spring Boot 应用的启动生命周期事件。ApplicationListener 和 @EventListener 更适合处理特定的应用生命周期事件,而 CommandLineRunner 和 ApplicationRunner 更适合在应用启动后执行一些初始化逻辑。SmartLifecycle 则适合需要更精细控制生命周期的情况。

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]