<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # SpringBoot – `CommandLineRunner`接口示例 > 原文: [https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/](https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/) 在應用程序初始化后,Spring Boot 的[`CommandLineRunner`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/CommandLineRunner.html)接口僅在應用程序生存期內運行一次代碼塊。 ## 如何使用`CommandLineRunner` 您可以通過三種方式使用`CommandLineRunner`界面: #### 1)使用`CommandLineRunner`作為`@Component` 這很容易。 ```java @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`中實現`CommandLineRunner` 這也是可能的。 示例代碼如下: ```java @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` ```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** ```java @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(); } } ``` 重要的是要注意,如果在`run(String... args)`方法內引發任何異常,這將導致上下文關閉并關閉應用程序。 因此,總是將有風險的代碼放在`try-catch`塊中。 ## 如果有多個`CommandLineRunner`接口實現,請使用`@Order` 您可能具有`CommandLineRunner`接口的多種實現。 默認情況下,spring boot 掃描其所有`run()`方法并執行。 但是,如果要在其中強加一些順序,請使用[`@Order`](https://docs.spring.io/spring-framework/docs/4.3.7.RELEASE/javadoc-api/org/springframework/core/annotation/Order.html?is-external=true)注解。 ```java @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 !!"); } } ``` 驗證日志。 ```java 2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !! 2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !! ``` ## 為什么要使用`CommandLineRunner`接口 * 命令行運行程序是一種有用的功能,可以在應用程序啟動后立即執行僅需運行一次的各種類型的代碼。 * 僅供參考,Spring Batch 依靠這些運行程序來觸發作業的執行。 * 我們可以利用依賴注入來發揮優勢,以便在`run()`方法實現中連接所需的任何依賴以及所需的任何方式。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看