在应用程序启动之前运行自定义代码的不同方法

Dar*_*nko 5 spring spring-boot

您能否描述在应用程序启动以进行数据初始化或其他操作之前运行自定义代码的不同方法?(比如ApplicationListenerCommandLineRunner等等)

它们之间有什么区别?在哪些情况下更适合使用它们?我不仅想知道一种方法,还想知道何时需要使用什么。

这是足够老的问题,有太多选项可以做到这一点:在 Spring Boot 启动后运行代码

如果问这个问题的地方不对,请指出我正确的地方。

Dar*_*nko 5

我知道什么选项:

  1. CommandLineRunner - 以字符串形式接收命令行参数

    @Slf4j
    @Component
    public class DemoCommandLineRunner implements CommandLineRunner {
    
        @Override
        public void run(String... args) {
            log.info("[CommandLineRunner] Args: " + Arrays.toString(args));
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. ApplicationRunner - 接收带有名称的命令行参数

    @Slf4j
    @Component
    public class DemoApplicationRunner implements ApplicationRunner {
    
        @Override
        public void run(ApplicationArguments args) {
            log.info("[ApplicationRunner] Args: ");
            nonOptionArgs(args);
            optionArgs(args);
        }
    
        private void nonOptionArgs(ApplicationArguments args) {
            args.getNonOptionArgs().forEach(log::info);
        }
    
        private void optionArgs(ApplicationArguments args) {
            args.getOptionNames().stream()
                    .map(args::getOptionValues)
                    .map(Objects::toString)
                    .forEach(log::info);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. ApplicationListener - 不同事件的监听器(对于每个事件自己的类)

    @Slf4j
    @Component
    public class DemoApplicationListener implements ApplicationListener<ApplicationEvent> {
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            logEvent(event);
        }
    
        private void logEvent(ApplicationEvent event) {
            log.info("[DemoApplicationListener] Event: " + event);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. @EventListener - 不同事件的监听器(一个 bean 中的多个事件)

    @Slf4j
    @Component
    public class DemoEventApplicationListener {
    
        @EventListener
        public void handleContextRefreshedEvent(ContextRefreshedEvent event) {
            logEvent(event);
        }
    
        @EventListener
        public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
            logEvent(event);
        }
    
        private void logEvent(ApplicationEvent event) {
            log.info("[DemoEventApplicationListener] Event: " + event);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. SmartLifecycle - 配置bean生命周期

    @Slf4j
    @Component
    public class DemoSmartLifecycle implements SmartLifecycle {
    
        private boolean isRunning;
    
        @Override
        public void start() {
            isRunning = true;
            log.info("[DemoSmartLifecycle]: Start");
        }
    
        @Override
        public void stop() {
            isRunning = false;
            log.info("[DemoSmartLifecycle]: Stop");
        }
    
        @Override
        public boolean isRunning() {
            return isRunning;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. SmartInitializingSingleton - 在单例预实例化阶段结束时触发

    @Slf4j
    @Component
    public class DemoSmartInitializingSingleton implements SmartInitializingSingleton {
    
        @Override
        public void afterSingletonsInstantiated() {
            log.info("[SmartInitializingSingleton] afterSingletonsInstantiated");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

Github 仓库: https: //github.com/venkaDaria/demo-bootstrap-spring