Spring boot CommandLineRunner接口使用例子


前言

Spring boot的CommandLineRunner接口主要用於實現在應用初始化后,去執行一段代碼塊邏輯,這段初始化代碼在整個應用生命周期內只會執行一次。

如何使用CommandLineRunner接口

我們可以用以下三種方式去使用CommandLineRunner接口:

1)和@Component注解一起使用

這種使用方式相當簡便,如下所示:

@Component
public class ApplicationStartupRunner implements CommandLineRunner {
    protected final Log logger = LogFactory.getLog(getClass());
 
    @Override
    public void run(String... args) throws Exception {
        logger.info("ApplicationStartupRunner run method Started !!");
    }
}

2)和@SpringBootApplication注解一起使用

這種使用方式也相當的簡單,參考代碼如下:

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }
 
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
 
 
    @Override
    public void run(String... args) throws Exception {
        logger.info("Application Started !!");
    }
}

3)聲明一個實現了CommandLineRunner接口的Bean

這種方式其實也大同小異,就是在SpringBootApplication里定義一個Bean,改Bean實現了CommandLineRunner接口,參考代碼如下:

ApplicationStartupRunner.java

public class ApplicationStartupRunner implements CommandLineRunner {
    protected final Log logger = LogFactory.getLog(getClass());
    @Override
    public void run(String... args) throws Exception {
        logger.info("Application Started !!");
    }
}

注冊ApplicationStartupRunner bean

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }
 
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
 
    @Bean
    public ApplicationStartupRunner schedulerRunner() {
        return new ApplicationStartupRunner();
    }
}

注意:在實現CommandLineRunner接口時,run(String… args)方法內部如果拋異常的話,會直接導致應用啟動失敗,所以,一定要記得將危險的代碼放在try-catch代碼塊里。

用@Order注解去設置多個CommandLineRunner實現類的執行順序

一個應用可能存在多個 CommandLineRunner接口實現類,如果我們想設置它們的執行順序,可以使用 @Order實現

@Order(value=3)
@Component
class ApplicationStartupRunnerOne implements CommandLineRunner {
    protected final Log logger = LogFactory.getLog(getClass());
 
    @Override
    public void run(String... args) throws Exception {
        logger.info("ApplicationStartupRunnerOne run method Started !!");
    }
}
 
@Order(value=2)
@Component
class ApplicationStartupRunnerTwo implements CommandLineRunner {
    protected final Log logger = LogFactory.getLog(getClass());
 
    @Override
    public void run(String... args) throws Exception {
        logger.info("ApplicationStartupRunnerTwo run method Started !!");
    }
}

輸出日志:

2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !!
2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !!

為什么要使用CommandLineRunner接口

  • 實現在應用啟動后,去執行相關代碼邏輯,且只會執行一次;
  • spring batch批量處理框架依賴這些執行器去觸發執行任務;
  • 我們可以在run()方法里使用任何依賴,因為它們已經初始化好了;

原文文鏈

Site4J


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM